簡體   English   中英

如果設備關閉,如何將通知保留在通知區域/欄中?

[英]How can keep notification in notification area/ bar if device shutdown?

我正在嘗試實施推送通知,我也收到了通知。 但是現在我需要在打開時在通知欄中進行通知。 也就是說,如果我在移動設備開啟時收到通知,那么我們在通知區域中看不到通知,如果設備關機,那么當我打開移動設備時,有時需要在通知欄上獲取該通知,我還有另一個要求,即,如果我在通知區域中刪除通知,則10分鍾后,我需要在通知區域/欄中獲取該通知。

我該怎么做?

  1. 您需要將通知內容保存在某些位置,例如SharePreference
  2. 使用此目標“ android.intent.action.BOOT_COMPLETED”啟動設備時,您會進行監聽,當廣播接收器被觸發時,您會從步驟1中再次讀取通知內容,即觸發通知。

很簡單,對:)

您可以將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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM