gpt4 book ai didi

android - 除非断点,否则通知不会更新

转载 作者:行者123 更新时间:2023-11-30 05:03:53 26 4
gpt4 key购买 nike

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

这是方法,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

最佳答案

我不知道到底发生了什么,但是在使用成功/最终通知更新通知之前使用 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());

关于android - 除非断点,否则通知不会更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54925097/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com