我正在实现 this代码,根据Google Developers Guide .
一切正常,我可以成功接收通知数据。
如果您检查 GCMIntentService.java 文件第 70 行,它会将此行回显到 LogCat:
I/GCM Demo(6653): Working... 1/5 @ 8656981
I/GCM Demo(6653): Working... 2/5 @ 8657982
I/GCM Demo(6653): Working... 3/5 @ 8658983
I/GCM Demo(6653): Working... 4/5 @ 8659983
I/GCM Demo(6653): Working... 5/5 @ 8660984
I/GCM Demo(6653): Completed work @ 8661985
I/GCM Demo(6653): Received: Bundle[{msg=messagehere, from=SENDERIDHERE, android.support.content.wakelockid=3, collapse_key=do_not_collapse}]
所以我的设备正在接收通知数据,但设备的状态栏中没有可见的通知。什么也没发生。
您能告诉我为什么这些推送通知不显示在我的设备上,而仅显示在 LogCat 中吗?
更新
- Manifest file
这是我的 sendNotification() 方法:
private void sendNotification(String msg) { 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) .setContentTitle("GCM Notification") .setStyle(new NotificationCompat.BigTextStyle() .bigText(msg)) .setContentText(msg); mBuilder.setContentIntent(contentIntent); mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build()); }
最佳答案
你能尝试一下这个方法吗...这个方法对我有用。虽然和你的几乎一样......
private void sendNotification(String msg)
{
int uniqueId = (int) (System.currentTimeMillis() & 0xfffffff);
mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP
| Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(this, uniqueId,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Hello")
.setStyle(new NotificationCompat.BigTextStyle().bigText("Hello"))
.setContentText("This is your notification content")
.setAutoCancel(true)
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(uniqueId, mBuilder.build());
}
唤醒手机是完全不同的事情。您可以通过以下方式使用 WakeLock 来完成此操作:
当您设置通知时:
WakeLock screenOn = ((PowerManager)getSystemService(POWER_SERVICE)).newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "test");
screenOn.acquire();
是的,当您不再需要唤醒锁时,不要忘记释放它(可能是 5 秒后或点击通知;根据您的需要):
screenOn.release();
关于java - 显示收到的推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26595041/