除非断点

时间:2019-02-28 11:50:58

标签: android android-notifications android-broadcastreceiver android-debug

我有一个叫BroadcastReceiver的服务。它工作正常,但是在我更改了用于处理通知ID的后端系统之后,最后的通知更新将更新通知以告知用户流程已完成。但是,除非我在处理这些通知的方法中的调试模式下放置断点,否则最终更新将无法通过。如果未放置断点或调试模式未处于活动状态,则最终通知更新将不会发生,并且通知将显示其最后状态,告知用户该过程尚未完成。

这是方法,Log.d()显示manager.notify()方法已被正确调用以更新通知以表示该过程已完成。

@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");
    }
}

预先感谢:P

1 个答案:

答案 0 :(得分:0)

我不知道发生了什么,但是在成功/最终通知更新之前,使用manager.cancel()取消了通知。

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());