我正在尝试实现推送通知,我也收到了通知。但是现在我需要在打开时通知栏中的通知。即,如果我在手机打开时收到通知,我们没有看到通知区域中的通知,如果设备关闭,那么当我打开手机时,有时需要在通知栏上获得该通知,我还有另一个要求,即,如果我在通知区域中删除通知,那么 10 分钟后我需要在通知区域/栏中获得该通知。
我怎样才能做到这一点?
我正在尝试实现推送通知,我也收到了通知。但是现在我需要在打开时通知栏中的通知。即,如果我在手机打开时收到通知,我们没有看到通知区域中的通知,如果设备关闭,那么当我打开手机时,有时需要在通知栏上获得该通知,我还有另一个要求,即,如果我在通知区域中删除通知,那么 10 分钟后我需要在通知区域/栏中获得该通知。
我怎样才能做到这一点?
简单,对:)
您可以将 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" />
在你的清单中BroadcastReceveiver
做引导注册的事情。BOOT_COMPLETED
一定时间后做东西使用AlarmManager
。