【问题标题】:Android Repeated Notifications not working When App is Closed关闭应用程序时Android重复通知不起作用
【发布时间】:2021-01-12 14:33:03
【问题描述】:

我想每天在特定时间发送通知。打开应用程序时代码正在运行。但是当它关​​闭并删除时,通知没有显示。我已经为此使用了广播接收器和服务。代码如下。谁能帮忙解决这个问题。

清单文件

<receiver
        android:name=".MyReceiver"
        android:enabled="true"
        android:exported="true" />
<service
        android:name=".MyService"
        android:enabled="true"
        android:exported="true" />

MyReceiver.java

public class MyReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    intent = new Intent(context, MyService.class);
    context.startService(intent);
}}

MyService.java

public class MyService extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    createNotification();
    return Service.START_STICKY;
}

private static final String NOTIFICATION_CHANNEL_ID = "Channel01";
private void createNotification() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        String name =  preferences.getString("name", "User");
        name = name.split(" ")[0];
        String namee = "Remainder";
        String description = "Remainder to update Wallet";
        int importance = NotificationManager.IMPORTANCE_HIGH;

        NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, namee, importance);
        notificationChannel.setDescription(description);

        Intent notifyIntent = new Intent(getApplicationContext(), MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 1, notifyIntent, 0);

        Notification notification = new Notification.Builder(getApplicationContext())
                .setContentTitle("Remainder")
                .setContentText("Hey " + name + ", Let's update your wallet")
                .setSmallIcon(R.drawable.wallet)
                .setChannelId(NOTIFICATION_CHANNEL_ID)
                .setLargeIcon(BitmapFactory.decodeResource(getApplicationContext().getResources(), R.drawable.wallet_new))
                .setContentIntent(pendingIntent)
                .build();

        NotificationManager notificationManager = (NotificationManager)getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.createNotificationChannel(notificationChannel);
        // Issue the notification.
        notificationManager.notify(1 , notification);
    }
}}

Activity.java

Intent notifyIntent = new Intent(getApplicationContext(), MyReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 1, notifyIntent, 0);
alarmManager = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, timeMilli, timeInterval, pendingIntent);

【问题讨论】:

    标签: java android android-studio mobile android-notifications


    【解决方案1】:

    您不应使用应用程序的任何部分来执行此操作。服务、JobScheduler 或 Work Manager 迟早会被系统杀死以防止电池耗尽。

    在我看来,发送重复通知的最佳方式是使用由外部 cron 作业触发的 firebase 云消息传递(例如,php on firebase 函数)。

    还要确保将通知发送到系统托盘而不是应用程序。为此,请使用 FCM DataMessages。数据消息传送到系统托盘并始终显示 - 即使服务未运行。

    【讨论】:

    • 感谢您的回复。在这个应用程序中,用户选择时间来获取通知作为剩余时间。因此必须使用本地通知。你有什么想法
    • 我认为您可以将应用程序 ID 与有关所选时间的信息一起存储在数据库中,然后每分钟运行一次 cron 并将当前时间与应用程序时间进行比较,然后将 FCM 发送到那些匹配的应用程序...
    猜你喜欢
    • 1970-01-01
    • 2016-11-19
    • 1970-01-01
    • 2022-01-13
    • 2021-03-08
    • 2018-03-21
    • 1970-01-01
    • 2018-03-15
    • 1970-01-01
    相关资源
    最近更新 更多