我正在使用 infobip PushNotification 我从客户端面板发送 PushNotification 但不接收通知并发送 toast "Error occurred:32" Invalid GCM sender ID 。我如何获得推送通知?
清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.push"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<!-- REQUIRED PERMISSIONS -->
<!-- Custom permission declared so that only your application can receive your notifications. -->
<permission
android:name="com.example.push.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<!-- Previously custom defined permission -->
<uses-permission android:name="com.example.push.permission.C2D_MESSAGE" />
<!-- Permission to receive push notifications -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<!-- Connect to the Infobip Push service -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- Using push notifications requires a Google account -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<!-- Keeps the processor from sleeping when a message is received -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<!-- Checks network state -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- REQUIRED PERMISSIONS -->
<!-- OPTIONAL PERMISSIONS -->
<!-- Needed for push notifications that contain VIBRATE flag. Optional, but recommended. -->
<uses-permission android:name="android.permission.VIBRATE" />
<!-- OPTIONAL PERMISSIONS -->
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- REQUIRED SERVICES -->
<service android:name="com.infobip.push.lib.InfobipPushService" />
<!-- REQUIRED SERVICES -->
<!-- REQUIRED RECEIVERS -->
<receiver
android:name="com.infobip.push.lib.InfobipPushReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.example.push" />
</intent-filter>
</receiver>
<receiver android:name="com.infobip.push.lib.InternalReceiver" />
<receiver
android:name=".MyPushReceiver"
android:permission="com.example.push.permission.C2D_MESSAGE" >
<intent-filter>
<action android:name="com.infobip.push.intent.REGISTERED_FOR_NOTIFICATIONS" />
<action android:name="com.infobip.push.intent.REGISTRATION_REFRESHED" />
<action android:name="com.infobip.push.intent.UNREGISTERED_FROM_NOTIFICATIONS" />
<action android:name="com.infobip.push.intent.NOTIFICATION_RECEIVED" />
<action android:name="com.infobip.push.intent.NOTIFICATION_OPENED" />
<action android:name="com.infobip.push.intent.ERROR" />
<category android:name="com.example.push" />
</intent-filter>
</receiver>
<!-- REQUIRED RECEIVERS -->
<!-- REQUIRED METADATA TAG -->
<meta-data
android:name="com.google.android.gms.version"
android:value="@+integer/google_play_services_version" />
<!-- REQUIRED METADATA TAG -->
</application>
</manifest>
MyPushReceiver 文件:
package com.example.push;
import android.content.Context;
import android.widget.Toast;
import com.infobip.push.AbstractPushReceiver;
import com.infobip.push.PushNotification;
public class MyPushReceiver extends AbstractPushReceiver {
@Override
public void onRegistered(Context context) {
Toast.makeText(context, "Successfully registered.", Toast.LENGTH_SHORT)
.show();
// TODO Auto-generated method stub
}
@Override
protected void onRegistrationRefreshed(Context context) {
Toast.makeText(context, "Registration is refreshed.",
Toast.LENGTH_SHORT).show();
}
@Override
public void onNotificationReceived(PushNotification notification,
Context context) {
Toast.makeText(context,
"Received notification: " + notification.toString(),
Toast.LENGTH_SHORT).show();
}
@Override
protected void onNotificationOpened(PushNotification notification,
Context context) {
Toast.makeText(context, "Notification opened.", Toast.LENGTH_LONG)
.show();
}
@Override
public void onUnregistered(Context context) {
Toast.makeText(context, "Successfully unregistered.",
Toast.LENGTH_SHORT).show();
}
@Override
public void onError(int reason, Context context) {
Toast.makeText(context, "Error occurred: " + reason, Toast.LENGTH_SHORT)
.show();
}
}
主要活动 :
package com.example.push;
import java.util.ArrayList;
import java.util.List;
import com.infobip.push.AbstractPushReceiver;
import com.infobip.push.PushNotification;
import com.infobip.push.PushNotificationManager;
import com.infobip.push.RegistrationData;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
public class MainActivity extends Activity {
PushNotificationManager manager ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
manager = new PushNotificationManager(this);
manager.isDebugModeEnabled();
manager.initialize("<_>", "<_>", "<_>");
manager.register();
// Intent startIntent = new Intent(this, MyPushReceiver.class);
// this.startService(startIntent);
}
private BroadcastReceiver receiver = new AbstractPushReceiver()
{
@Override
public void onRegistered(Context context) {
// TODO Auto-generated method stub
}
@Override
public void onNotificationReceived(PushNotification notification,
Context context) {
// TODO Auto-generated method stub
}
@Override
public void onUnregistered(Context context) {
// TODO Auto-generated method stub
}
@Override
public void onError(int reason, Context context) {
// TODO Auto-generated method stub
}
};
@Override
protected void onResume() {
super.onResume();
IntentFilter filter = new IntentFilter();
filter.addAction("com.infobip.push.intent.REGISTERED_FOR_NOTIFICATIONS");
filter.addAction("com.infobip.push.intent.REGISTRATION_REFRESHED");
filter.addAction("com.infobip.push.intent.UNREGISTERED_FROM_NOTIFICATIONS");
filter.addAction("com.infobip.push.intent.NOTIFICATION_RECEIVED");
filter.addAction("com.infobip.push.intent.NOTIFICATION_OPENED");
filter.addAction("com.infobip.push.intent.ERROR");
filter.addCategory(getPackageName());
registerReceiver(receiver, filter); //Register receiver
}
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(receiver); //Unregister receiver
}
public void onSubscriptionClick(View v) {
String userID = "<senderid>";
String phoneNumber = "385951112222";
List channels = new ArrayList();
channels.add("sport");
channels.add("news");
channels.add("weather");
//Perform registration
RegistrationData registrationData = new RegistrationData();
registrationData.setUserId(userID);
registrationData.setPhoneNumber(phoneNumber);
registrationData.setChannels(channels);
manager.register(registrationData); //registrationData is optional
}
}