如果应用程序未 运行 (Android) 一周未打开,则推送通知

Push notification if app has not open for a week when the app is not running (Android)

我正在编写代码来为我的应用推送通知,但我不知道如何显示此通知。当用户一周没有使用我的应用程序时,即使应用程序不运行,应用程序仍然可以发送通知。请帮我。谢谢你。 这是我发送通知的代码。

private void showNotification(){
    final NotificationManager mgr=
            (NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);

    //Notice message
    Notification note=new Notification(R.mipmap.ic_launcher,
            "Notification is coming!",
            System.currentTimeMillis());

    // This pending intent will open class after notification click
    PendingIntent i=PendingIntent.getActivity(this, 0,
            new Intent(this, ResultActivity.class),
            0);

    //After swipe down notification
    note.setLatestEventInfo(this, "Title",
            "Context", i);

    //After uncomment this line you will see number of notification arrived
    //note.number=2;
    mgr.notify(notificationID, note);
}

基于Send notification once in a week你只需要在应用打开时保存,然后在以后7天后,用户每次打开应用时都会收到通知。

您还可以使用唯一标识符保存每个警报,以便以后禁用它。

类似于:

PendingIntent pendingIntent = PendingIntent.getService(context, _id, intent, 0);
alarmManager.set(AlarmManager.RTC_WAKEUP, date.getTimeInMillis(), pendingIntent);

或取消它:

alarmManager.cancel(pendingIntent);

_id是取消当前报警的按键。

请记住,使用 AlarmManager 设备重启后所有警报都会消失。