2014-10-27 144 views
0

根据Google Developers Guide,我正在实施this代码。显示接收的推送通知

一切都好,我可以成功接收通知数据。

如果检查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中?

最新通报

  1. Manifest file
  2. 这是我的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()); 
    } 
    
+0

将您的代码发布到创建通知(您链接的代码中的'sendNotification')的地方。 – ianhanniballake 2014-10-27 19:20:47

+0

尝试在第106行使用'(int)(System.currentTimeMillis()&0xfffffff);'代替NOTIFICATION_ID'。只是疯狂的猜测。还要在清单文件中尝试使用完整路径作为意图服务类。 – Rohit5k2 2014-10-27 19:22:54

+0

@ianhanniballake发布。谢谢。 – Eray 2014-10-27 19:23:01

回答

1

你能试试这个方法....这一个是为我工作。虽然它几乎与你的一样...

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 screenOn = ((PowerManager)getSystemService(POWER_SERVICE)).newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "test"); 
screenOn.acquire(); 

是的,不要忘记释放唤醒锁定当你不需要它了(可能是后5秒或点击通知;根据您的需要):

screenOn.release(); 
+0

它的工作!现在我试图区别:) – Eray 2014-10-27 19:40:26

+0

区别在于添加标志。默认的例子是有点旧,并没有更新。如果它解决了你的问题,你能接受这个答案吗?:D – Rohit5k2 2014-10-27 19:41:50

+0

为什么它不锁醒手机? (当然我会接受它,但在它之前,我有一些问题:)) – Eray 2014-10-27 19:44:10

0

它看起来像(在你的Android清单文件),该服务可未启用

请尝试更换<service android:name=".GcmIntentService" />

<service android:enabled="true" android:name=".GcmIntentService" /> 
+0

Kyle,我试过了。没有改变。 – Eray 2014-10-27 19:25:42

+0

'android:enabled =“true”'对于简单通知不是必需的。没有它就行得通。 – Rohit5k2 2014-10-27 19:25:58

+0

不是吗?有趣。我正在使用API​​ 19,并且清单中没有包含“启用”的问题。 – Kyle 2014-10-27 19:28:05