Notifications don't update unless Breakpoint

问题: I have a service that calls a BroadcastReceiver. It was working fine but after I changed the back-end system for handling notification ids, the final notification update wh...

问题:

I have a service that calls a BroadcastReceiver. It was working fine but after I changed the back-end system for handling notification ids, the final notification update which should update a notification to tell the user that a process has finished. However, that final update would not pass through unless I place a breakpoint in debugging mode in the method handling those notifications. If a breakpoint is not placed or debug mode is not active, that final notification update would not happen and the notification would display its last state which tells the user that the process has not finished, which is not the case.

Here's the method, and the Log.d()s show that the manager.notify() method was called properly to update the notification to signify that the process has finished.

@Override
public void onReceive(Context context, Intent intent) {

    Download data = null;
    try {
        data = (Download) intent.getSerializableExtra(Commons.ARGS.DATA);
    } catch (ClassCastException | NullPointerException e) {
        e.printStackTrace();
    }

    switch (intent.getIntExtra(Commons.ARGS.RESULT, Commons.ARGS.FAILED)) {
        case Commons.ARGS.DESTROY:
            Log.w(TAG, "Service was destroyed");

            if (notifications && manager != null) {
                Download[] remaining = (Download[]) intent.getParcelableArrayExtra(Commons.ARGS.DATA);

                if (remaining == null) break;
                for (Download d : remaining) {
                    // Download has failed since service was destroyed; was never called and has no relations to this case. manager.notify() is called here to update the notification as "cancelled" btw
                }
            }
            break;
        case Commons.ARGS.ERR_LOAD:
            Log.w(TAG, "Failed to load queue");
            break;
    }

    if (data == null) return;

    if (notifications) {
        Intent i = new Intent(context, MainActivity.class);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(activity, Commons.Notif.DOWNLOAD_PROGRESS)
                .setContentIntent(PendingIntent.getActivity(activity, 0, i, 0))
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setSmallIcon(R.drawable.ic_notif)
                .setContentTitle(data.title)
                .setColor(context.getResources().getColor(R.color.Accent))
                .setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_SUMMARY)
                .setGroup(CHANNEL_ID);

        if (manager == null) {
            manager = (NotificationManager) activity.getSystemService(Context.NOTIFICATION_SERVICE);

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "Downloads in Progress", NotificationManager.IMPORTANCE_DEFAULT);
                channel.setDescription("All downloads are displayed here");
                channel.enableLights(false);
                channel.enableVibration(false);
                manager.createNotificationChannel(channel);
            }
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            builder.setChannelId(CHANNEL_ID);
        }

        // Notification builder styling is handled here

        manager.notify(0, new NotificationCompat.Builder(activity, Commons.Notif.DOWNLOAD_PROGRESS)
            .setChannelId(CHANNEL_ID)
            .setSmallIcon(R.drawable.ic_notif)
            .setGroup(CHANNEL_ID)
            .setColor(activity.getResources().getColor(R.color.Accent))
            .setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_SUMMARY)
            .setGroupSummary(true)
            .setContentText("All downloads have finished.")
            .setContentTitle("Done")
            .build());

        manager.notify(DOWNLOAD_BASE_ID | (data.id & 0x00FF_FFFF), builder.build());
        Log.d(TAG, "Notification updated");
    }
}

Thanks in advance :P


回答1:

I do not know what exactly is happening but canceling the notification using manager.cancel() before updating it with the success/final notification solved the problem.

int id = // Notification id
if (success) {
    Log.d(TAG, "Success Notification updated");
    manager.cancel(id);
} else Log.d(TAG, "Notification updated");
manager.notify(id, builder.build());
  • 发表于 2019-03-02 14:49
  • 阅读 ( 201 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除