簡體   English   中英

android中的本地通知在設備重啟時使用Alarmmanager

[英]Local Notification in android using Alarmmanager on Reboot of device

我創建了一個本地通知,並在 mainactivity 中觸發了它。

主活動代碼如下:

import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {
    public static final String NOTIFICATION_CHANNEL_ID = "10001";
    private final static String default_notification_channel_id = "default";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //here i create and call the notification, it will trigger every 30 seconds or so
        scheduleNotification(getNotification("30 second delay"), 30000);
    }

    public void scheduleNotification(Notification notification, int delay) {
        //here is the intent which uses the MyNotificationPublisher.class
        Intent notificationIntent = new Intent(this, MyNotificationPublisher.class);

        notificationIntent.putExtra(MyNotificationPublisher.NOTIFICATION_ID, 1);
        notificationIntent.putExtra(MyNotificationPublisher.NOTIFICATION, notification);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        long futureInMillis = SystemClock.elapsedRealtime() + delay;
        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        assert alarmManager != null;
        //here i am starting the alarm manager with setInexactRepeating to continuesly run the notification every 30 seconds
        alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, futureInMillis, 3000 * 10, pendingIntent);;

    }

    public Notification getNotification(String content) {
        //here i setup the notification
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, default_notification_channel_id);
        builder.setContentTitle("Scheduled Notification");
        builder.setContentText(content);
        builder.setSmallIcon(R.drawable.ic_launcher_foreground);
        builder.setAutoCancel(true);
        builder.setChannelId(NOTIFICATION_CHANNEL_ID);
        return builder.build();
    }
}

下面是MyNotificationPublisher類的代碼:

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import static app.tutorialspoint.com.notifyme.MainActivity.NOTIFICATION_CHANNEL_ID;

public class MyNotificationPublisher extends BroadcastReceiver {

    public static String NOTIFICATION_ID = "notification-id";
    public static String NOTIFICATION = "notification";
    public void onReceive(Context context, Intent intent) {
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = intent.getParcelableExtra(NOTIFICATION);

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            int importance = NotificationManager.IMPORTANCE_HIGH;
            NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", importance);
            assert notificationManager != null;
            notificationManager.createNotificationChannel(notificationChannel);
        }
        int id = intent.getIntExtra(NOTIFICATION_ID, 0);
        assert notificationManager != null;
        notificationManager.notify(id, notification);
    }
}

這是接收方.MyNotificationPublisher所在的清單 xml

<? xml version = "1.0" encoding = "utf-8" ?>
<manifest xmlns: android = "http://schemas.android.com/apk/res/android"
   package= "app.tutorialspoint.com.notifyme" >
   <uses-permission android :name = "android.permission.VIBRATE" />
   <application
      android :allowBackup = "true"
      android :icon = "@mipmap/ic_launcher"
      android :label = "@string/app_name"
      android :roundIcon = "@mipmap/ic_launcher_round"
      android :supportsRtl = "true"
      android :theme = "@style/AppTheme" >
      <activity android :name = ".MainActivity" >
         <intent-filter>
            <action android :name = "android.intent.action.MAIN" />
            <category android :name = "android.intent.category.LAUNCHER" />
         </intent-filter>
      </activity>
      <receiver android :name = ".MyNotificationPublisher" />
   </application>
</manifest>

代碼正在運行,並且每 30 秒觸發一次通知,但我需要實現以下目標。

  1. 當用戶關閉應用程序時,我需要開始顯示此通知。 不是在應用程序運行時。
  2. 如何在設備重新啟動后使通知一次又一次地運行(我在“設備重新啟動時啟動警報”部分嘗試了此頁面說明https://developer.android.com/training/scheduling/alarms ,但我這樣做了不知道如何啟動警報和通知再次運行,因為每次設備關閉所有活動警報都會被取消。我嘗試使用“android.permission.RECEIVE_BOOT_COMPLETED”,因為我是在https://developer.android.com 上的網頁指示的/training/scheduling/alarms然后調用 public void scheduleNotification (Notification notification , int delay) 使用 MainActivity mActivity = new MainActivity(); mActivity.scheduleNotification(mActivity.getNotification( "30 second delay" ) , 30000 ); 從 MyNotificationPublisher類,但應用程序崩潰。)

任何想法或解決方案?

您需要在清單文件中為您的接收器添加意圖過濾器。 試試下面的代碼。

<receiver android :name = ".MyNotificationPublisher">
   <intent-filter> 
        <action android:name="android.intent.action.BOOT_COMPLETED"></action> 
   </intent-filter>
</receiver> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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