显示已接收的推送通知

时间:2014-10-27 19:10:04

标签: java android notifications push-notification

根据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中显示吗?

更新

  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());
    }
    

2 个答案:

答案 0 :(得分: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来实现:

设置通知时:

WakeLock screenOn = ((PowerManager)getSystemService(POWER_SERVICE)).newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "test");
screenOn.acquire();

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

screenOn.release();

答案 1 :(得分:0)

看起来(在您的Android清单文件中)该服务可能尚未启用

请尝试将<service android:name=".GcmIntentService" />替换为:

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