我正在使用infobip PushNotification我从clientpanel发送PushNotification但没有收到通知并发送toast“发生错误:32”无效的GCM发件人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();
}
}
MainActivity:
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
}
}
答案 0 :(得分:1)
你有GCMIntentService类吗?如下面的代码
public class GCMIntentService extends GCMBaseIntentService {
private static final String TAG = "GCMIntentService";
public GCMIntentService() {
super(SENDER_ID);
}
@Override
protected void onRegistered(Context context, String registrationId) {
Log.i(TAG, "Device registered: regId = " + registrationId);
// displayMessage(context, getString(R.string.gcm_registered));
ServerUtilities.register(context, registrationId);
}
@Override
protected void onUnregistered(Context context, String registrationId) {
Log.i(TAG, "Device unregistered");
// displayMessage(context, getString(R.string.gcm_unregistered));
if (GCMRegistrar.isRegisteredOnServer(context)) {
ServerUtilities.unregister(context, registrationId);
} else {
// This callback results from the call to unregister made on
// ServerUtilities when the registration to the server failed.
Log.i(TAG, "Ignoring unregister callback");
}
}
@Override
protected void onMessage(Context context, Intent intent) {
Log.i(TAG, "Received message");
// String message = getString(R.string.gcm_message);
String message = intent.getStringExtra("message");
Log.i("message", "" + message);
// displayMessage(context, message);
// notifies user
generateNotification(context, message);
}
@Override
protected void onDeletedMessages(Context context, int total) {
Log.i(TAG, "Received deleted messages notification");
String message = getString(R.string.gcm_deleted, total);
// displayMessage(context, message);
// notifies user
generateNotification(context, message);
}
@Override
public void onError(Context context, String errorId) {
Log.i(TAG, "Received error: " + errorId);
// displayMessage(context, getString(R.string.gcm_error, errorId));
}
@Override
protected boolean onRecoverableError(Context context, String errorId) {
// log message
Log.i(TAG, "Received recoverable error: " + errorId);
// displayMessage(context, getString(R.string.gcm_recoverable_error,
// errorId));
return super.onRecoverableError(context, errorId);
}
/**
* Issues a notification to inform the user that server has sent a message.
*/
private static void generateNotification(Context context, String message) {
int icon = R.drawable.ic_stat_gcm;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context, MainActivity.class);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
}
}
答案 1 :(得分:0)
关于无效的发件人ID,StackOverflow上已有一些答案可以帮助您... 例如:Android GCM SENDER_ID, how to get it?或 GCM Invalid sender error
关于Infobip的推送服务,我发现您已经两次调用register()方法。第一次创建活动时(没有注册数据),第二次点击按钮(使用RegistrationData)。你只需要这样做一次,因为一旦你注册了推送,就没有必要重新注册。
您的问题是在initialize("<senderId>", "<applicationId>", "<applicationSecret>")
方法中调用错误的senderID参数。
发件人ID = Google项目编号
在此处查看如何获取项目编号:https://push.infobip.com/docs/platforms/android#androidgcm
您的活动示例:
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.setDebugModeEnabled(true);
//REPLACE "<senderId>" WITH GOOGLE PROJECT NUMBER!
manager.initialize("<senderId>", "<applicationId>", "<applicationSecret>")
}
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);
if (!manager.isRegistered()) {
manager.register(registrationData); //registrationData is optional
}
}
}