java - 显示收到的推送通知

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

最佳答案

你能尝试一下这个方法吗...这个方法对我有用。虽然和你的几乎一样......

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/

相关文章:

java - 如何在多个Spring Boot存储库实例之间切换

java - 是否可以在 Android View 上查询正在运行/待处理的动画?

android - ListFragment 不显示当前时间的变化

java - Android中如何实现多个定时通知

Android O 报告通知未发布到 channel - 但它是

java - Apache Camel + QPid 将 messageid 设置为 UUID 不起作用

java - HashMap 和 LinkedHashMap 类中 keySet().iterator().next() 的时间复杂度?

java - hibernate 异常 : Could not parse mapping document

android - 范围内的 EditText 值

ios - 使用 Swift 3 中的日期选择器中的触发日期安排每周可重复的本地通知