Java 显示收到的推送通知
我正在执行代码,根据 一切正常,我可以成功接收通知数据 如果您检查gcminentservice.java文件的第70行,它会将这几行回显到LogCat:Java 显示收到的推送通知,java,android,notifications,push-notification,Java,Android,Notifications,Push Notification,我正在执行代码,根据 一切正常,我可以成功接收通知数据 如果您检查gcminentservice.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 @ 8659
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中吗
更新
替换为:
<service android:enabled="true" android:name=".GcmIntentService" />
你能试试这个方法吗。。。。这个对我有用。虽然它几乎和你的一样
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();
是的,当您不再需要wakelock时,不要忘记释放它(可能在5秒后或点击Notificationon;根据您的需要):
在创建通知的位置发布代码(在链接的代码中使用sendNotification)。尝试使用此
(int)(System.currentTimeMillis()&0xfffff)代码>代替第106行的通知ID。只是胡乱猜测。还可以尝试在清单文件中使用intent服务类的完整路径。@ianhanniballake已发布。谢谢。@Rohit5k2,试过了。没有更改。您是否尝试使用gcminentservice.java的完整路径?android:enabled=“true”
对于简单通知不是必需的。没有它很好用,不是吗?有趣。我使用的是API 19,但清单中没有启用
。原因一定是其他原因。没有这一点,它是有效的…:)不同之处在于添加了标志。默认示例有点旧,尚未更新。如果它解决了你的问题,你能接受这个答案吗如果手机锁上了,为什么它不会醒过来?(当然我会接受,但在此之前我有一些问题:)
WakeLock screenOn = ((PowerManager)getSystemService(POWER_SERVICE)).newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "test");
screenOn.acquire();
screenOn.release();