【问题标题】:How can keep notification in notification area/ bar if device shutdown?如果设备关闭,如何在通知区域/栏中保留通知?
【发布时间】:2016-04-07 06:56:22
【问题描述】:

我正在尝试实现推送通知,我也收到了通知。但是现在我需要在打开时通知栏中的通知。 IE。如果我在手机打开时收到通知,我们没有看到通知区域中的通知,如果设备关闭,那么当我打开手机时,有时需要在通知栏上获得该通知,我还有另一个要求,即如果我在通知区域中删除通知,那么 10 分钟后我需要在通知区域/栏中收到该通知。

我怎样才能做到这一点?

【问题讨论】:

    标签: android notifications notification-area


    【解决方案1】:
    1. 您需要将通知内容保存在SharePreference等位置。
    2. 您在使用此 Intent“android.intent.action.BOOT_COMPLETED”启动设备时进行监听,当广播接收器被触发时,您从第 1 步读取通知内容,再次触发通知。

    简单,对:)

    【讨论】:

      【解决方案2】:

      您可以将 PendingIntent 与 FLAG_ONE_SHOT 一起使用:

      private void sendNotification(String from, String message) {
              Bundle bundle = new Bundle();
              Intent intent = new Intent(this, ChatActivity.class);
              intent.putExtra("INFO", bundle);
              PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
              Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
              NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                      .setContentTitle(from)
                      .setSound(defaultSoundUri)
                      .setContentText(message)
                      .setSmallIcon(R.drawable.ic_notification)
                      .setAutoCancel(true)
                      .setContentIntent(pendingIntent);
      
              NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
      
              notificationManager.notify(0, builder.build());
          }
      

      记住在清单文件中设置 WAKE_LOCK 权限:

      <uses-permission android:name="android.permission.WAKE_LOCK" />
      

      【讨论】:

      • 不,它不工作。它没有在开机后弹出通知区域
      【解决方案3】:

      要在您的清单中注册一个BroadcastReceveiverBOOT_COMPLETED 的启动操作。

      要在一段时间后做一些事情,请使用AlarmManager

      【讨论】:

      • 我正在处理 GCM 推送通知,那么警报管理器和广播接收器是什么关系
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-27
      相关资源
      最近更新 更多