【发布时间】:2015-10-05 01:51:07
【问题描述】:
我有我设备的注册 ID,当我尝试从我的服务器向我的应用发送推送通知时,我收到了成功消息:
{"multicast_id":SOME_ID,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:SOME_ID"}]}
但我的应用没有显示任何通知或警报。我在网络上尝试了超过 6 种不同的指南,但仍然找不到从我的应用接收此通知的方法。
即使用户没有打开应用程序,如何接收此通知并将其作为推送通知显示在我的应用程序中?
public class GcmIntentService extends IntentService {
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
public GcmIntentService() {
super("GcmIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class), 0);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("New Message!");
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
}
广播接收器
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
ComponentName comp = new ComponentName(context.getPackageName(),
GcmIntentService.class.getName());
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
【问题讨论】:
-
可能推送正在发送到您的应用程序,但您没有正确处理它。如果您不共享您的某些应用程序代码,我无法推断出其他任何内容。您的清单中是否添加了适当的权限?
-
请发布您用于 IntentService 的代码
-
是的,关于权限我确定,但我不知道如何处理我发送的通知。尝试使用我在网上找到的所有内容,但没有任何效果。
-
@AndroidEnthusiast 我发布了它,但我的主要活动中没有任何相关内容
-
请同时添加广播接收器的代码
标签: android google-cloud-messaging