[英]How can keep notification in notification area/ bar if device shutdown?
我正在嘗試實施推送通知,我也收到了通知。 但是現在我需要在打開時在通知欄中進行通知。 也就是說,如果我在移動設備開啟時收到通知,那么我們在通知區域中看不到通知,如果設備關機,那么當我打開移動設備時,有時需要在通知欄上獲取該通知,我還有另一個要求,即,如果我在通知區域中刪除通知,則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" />
要在啟動時進行BOOT_COMPLETED
,請在清單BOOT_COMPLETED
BroadcastReceveiver
注冊到BOOT_COMPLETED
。
要在一定時間后執行操作,請使用AlarmManager
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.