简体   繁体   English

显示收到的推送通知

[英]Displaying Received Push Notifications

I'm implementing this codes , according to Google Developers Guide . 根据Google开发者指南 ,我正在实施这些代码。

Everything is ok , I can receive notification datas successfully. 一切正常,我可以成功接收通知数据。

If you check GCMIntentService.java file 70th line, it's echoing this lines to LogCat : 如果您检查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}]

So my device is receiving notification data, but there is no visible notification in device's status bar. 因此我的设备正在接收通知数据,但是设备状态栏中没有可见的通知。 Nothing happening. 没事

Can you tell me why these push notifications aren't displayed on my device, only in LogCat? 您能告诉我为什么这些推送通知仅在LogCat中不显示在我的设备上吗?

UPDATES 更新

  1. Manifest file 清单文件
  2. This is my sendNotification() method : 这是我的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()); } 

Could you try this method.... This one is working for me. 您可以尝试这种方法吗?...这个对我有用。 Although its almost same as yours... 虽然与您几乎一样...

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

Waking up the phone is quite different matter. 唤醒电话是完全不同的事情。 You can do it using a WakeLock this way: 您可以通过以下方式使用WakeLock进行操作:

When you set your notification: 设置通知时:

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

And yes, DON'T FORGET TO release the wakelock when you don't need it anymore (may be after 5 seconds or on tap of notificaton; as required per your need): 是的,不要忘记在不再需要唤醒锁时释放唤醒锁(可能在5秒钟后或点击通知;根据您的需要):

screenOn.release();

It looks like (in your android manifest file) that the service may not have been enabled 看起来(在您的android清单文件中)该服务可能尚未启用

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM