没有收到推送通知
not getting push notification
搜索 2 天后,应用在 Whosebug 上找到的解决方案无法解决我的应用程序中的问题。我没有在应用程序中收到推送通知。我正在使用 wp gcm 从 wordpress 发送消息,从 gcm 服务器获得响应:
{"multicast_id":8254312412899412709,"success":2,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1439464061399302%7e2c8c8ef9fd7ecd"},{"message_id":"0:1439464061399862%50b5570df9fd7ecd"}]}
在 logcat 中显示
08-13 15:36:50.752: V/GCMBroadcastReceiver(11517): onReceive: com.google.android.c2dm.intent.RECEIVE
08-13 15:36:50.752: V/GCMBroadcastReceiver(11517): GCM IntentService class: com.itcombine.saidlist.GCMIntentService
08-13 15:36:50.762: V/GCMBaseIntentService(11517): Acquiring wakelock
下面是我的代码:
GcmBroadcastReceiver:-
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
private static final String TAG = "GcmBroadcastReceiver";
@Override
public void onReceive(Context context, Intent intent) {
// Explicitly specify that GcmIntentService will handle the intent.
ComponentName comp = new ComponentName(context.getPackageName(),
GcmIntentService.class.getName());
// Start the service, keeping the device awake while it is launching.
startWakefulService(context, (intent.setComponent(comp)));
Log.d(TAG, "Got message");
setResultCode(Activity.RESULT_OK);
}
}
GcmIntentService:
public class GcmIntentService extends IntentService {
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
String TAG = "Px GCM IntentService";
String newms;
public GcmIntentService() {
super("GcmIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
// The getMessageType() intent parameter must be the intent you received
// in your BroadcastReceiver.
String messageType = gcm.getMessageType(intent);
Log.d(TAG, "Got message: " + messageType + ", gcm=" + gcm.toString());
if (!extras.isEmpty()) {
if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
sendNotification("Send error: " + extras.toString());
} else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
sendNotification("Deleted messages on server: " + extras.toString());
// If it's a regular GCM message, do some work.
} else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
// Post notification of received message.
newms = extras.getString("message").toString();
sendNotification("Received: " + newms);;
Log.i(TAG, "Received: " + extras.toString() + ", message=" + newms);
}
}
GcmBroadcastReceiver.completeWakefulIntent(intent);
Log.d(TAG, "Done handling message");
}
private void sendNotification(String msg) {
mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("alert", "1");
intent.putExtra("msg",newms);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,intent, 0);
Uri uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("GCM Notification")
.setSound(uri)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
}
AndroidManifest.xml :
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- GCM connects to Internet Services. -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- GCM 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" />
<!-- Creates a custom permission so only this app can receive its messages. -->
<permission
android:name="com.itcombine.saidlist.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.itcombine.saidlist.permission.C2D_MESSAGE" />
<!-- This app has permission to register and receive data message. -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<!-- Network State Permissions to detect Internet status -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- Permission to vibrate -->
<uses-permission android:name="android.permission.VIBRATE" />
<receiver
android:name="com.google.android.gcm.GCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<!-- Receives the actual messages. -->
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<!-- Receives the registration id. -->
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.itcombine.saidlist" />
</intent-filter>
</receiver>
<service android:name=".GCMIntentService" />
我在 SO 的其他一些问题中发现了同样的问题,但他们提供的解决方案对我不起作用。因为他们已经给出文件应该在同一个包中,但我只有一个包。请帮忙。提前致谢。
Google 建议不再使用 WakefulBroadcastReceiver,而是在 Android 上使用 GCMReceiver 和 GcmListenerService。进行此更改可能会解决您的问题
搜索 2 天后,应用在 Whosebug 上找到的解决方案无法解决我的应用程序中的问题。我没有在应用程序中收到推送通知。我正在使用 wp gcm 从 wordpress 发送消息,从 gcm 服务器获得响应:
{"multicast_id":8254312412899412709,"success":2,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1439464061399302%7e2c8c8ef9fd7ecd"},{"message_id":"0:1439464061399862%50b5570df9fd7ecd"}]}
在 logcat 中显示
08-13 15:36:50.752: V/GCMBroadcastReceiver(11517): onReceive: com.google.android.c2dm.intent.RECEIVE
08-13 15:36:50.752: V/GCMBroadcastReceiver(11517): GCM IntentService class: com.itcombine.saidlist.GCMIntentService
08-13 15:36:50.762: V/GCMBaseIntentService(11517): Acquiring wakelock
下面是我的代码:
GcmBroadcastReceiver:-
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
private static final String TAG = "GcmBroadcastReceiver";
@Override
public void onReceive(Context context, Intent intent) {
// Explicitly specify that GcmIntentService will handle the intent.
ComponentName comp = new ComponentName(context.getPackageName(),
GcmIntentService.class.getName());
// Start the service, keeping the device awake while it is launching.
startWakefulService(context, (intent.setComponent(comp)));
Log.d(TAG, "Got message");
setResultCode(Activity.RESULT_OK);
}
}
GcmIntentService:
public class GcmIntentService extends IntentService {
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
String TAG = "Px GCM IntentService";
String newms;
public GcmIntentService() {
super("GcmIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
// The getMessageType() intent parameter must be the intent you received
// in your BroadcastReceiver.
String messageType = gcm.getMessageType(intent);
Log.d(TAG, "Got message: " + messageType + ", gcm=" + gcm.toString());
if (!extras.isEmpty()) {
if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
sendNotification("Send error: " + extras.toString());
} else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
sendNotification("Deleted messages on server: " + extras.toString());
// If it's a regular GCM message, do some work.
} else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
// Post notification of received message.
newms = extras.getString("message").toString();
sendNotification("Received: " + newms);;
Log.i(TAG, "Received: " + extras.toString() + ", message=" + newms);
}
}
GcmBroadcastReceiver.completeWakefulIntent(intent);
Log.d(TAG, "Done handling message");
}
private void sendNotification(String msg) {
mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("alert", "1");
intent.putExtra("msg",newms);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,intent, 0);
Uri uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("GCM Notification")
.setSound(uri)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
}
AndroidManifest.xml :
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- GCM connects to Internet Services. -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- GCM 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" />
<!-- Creates a custom permission so only this app can receive its messages. -->
<permission
android:name="com.itcombine.saidlist.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.itcombine.saidlist.permission.C2D_MESSAGE" />
<!-- This app has permission to register and receive data message. -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<!-- Network State Permissions to detect Internet status -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- Permission to vibrate -->
<uses-permission android:name="android.permission.VIBRATE" />
<receiver
android:name="com.google.android.gcm.GCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<!-- Receives the actual messages. -->
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<!-- Receives the registration id. -->
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.itcombine.saidlist" />
</intent-filter>
</receiver>
<service android:name=".GCMIntentService" />
我在 SO 的其他一些问题中发现了同样的问题,但他们提供的解决方案对我不起作用。因为他们已经给出文件应该在同一个包中,但我只有一个包。请帮忙。提前致谢。
Google 建议不再使用 WakefulBroadcastReceiver,而是在 Android 上使用 GCMReceiver 和 GcmListenerService。进行此更改可能会解决您的问题