gpt4 book ai didi

android - 即使提供了通知 ID,也不会在单击操作按钮时删除通知

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:57:52 24 4
gpt4 key购买 nike

我发送的通知有 2 个操作按钮,即“接受”和“拒绝”。

我正在关注这个 Github repo .

当用户点击“接受”时,会检查某些条件并相应地执行逻辑。

UPDATE 2.0 - 问题是在单击“接受”按钮后,操作成功进行但通知并未从状态栏中消失,因为此处生成的 ID:m = (new Random()).nextInt(10000); 与这里不同:actionIntent.putExtra("id", NotificationARBroadcastReceiver.m); 每一次!

通知代码如下:

Intent notificationIntent = new Intent(getBaseContext(), NotificationARBroadcastReceiver.class);
notificationIntent.putExtra(NotificationARBroadcastReceiver.NOTIFICATION, getNotificationNewRequestService());
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), m, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 0, pendingIntent);

这是 getNotificationNewRequestService():

private Notification getNotificationNewRequestService() {

mBuilder =
new NotificationCompat.Builder(getBaseContext())
.setSmallIcon(R.mipmap.app_icon_1)
.setContentTitle("Title")
.setContentText("text...");

Intent resultIntent = new Intent(getBaseContext(), Profile.class);

PendingIntent resultPendingIntent =
PendingIntent.getActivity(
getBaseContext(),
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);

// for action button
Intent actionIntent = new Intent(getBaseContext(), MyBroadcastSender.class);
actionIntent.putExtra("id", NotificationARBroadcastReceiver.m);
PendingIntent actionPendingIntent = PendingIntent
.getBroadcast(getBaseContext(),
0, actionIntent, PendingIntent.FLAG_UPDATE_CURRENT);

mBuilder.setAutoCancel(true);
mBuilder.setContentIntent(resultPendingIntent);
mBuilder.addAction(R.drawable.ic_accepted_request_black_24dp, "Accept", actionPendingIntent);
mBuilder.addAction(R.drawable.ic_close_black_24dp, "Reject", null);

return mBuilder.build();
}

这是 NotificationARBroadcastReceiver.java 文件:

public class NotificationARBroadcastReceiver extends BroadcastReceiver {

public static String NOTIFICATION = "notification";
public static NotificationManager mNotifyMgr;
public static int m;

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

m = (new Random()).nextInt(10000);
Log.d("mMain", String.valueOf(m));

mNotifyMgr =
(NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);

Notification notification = intent.getParcelableExtra(NOTIFICATION);
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
mNotifyMgr.notify(m, notification);

}
}

这是 MyBroadcastSender.java 文件:

public class MyBroadcastSender extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {

Toast.makeText(context, "Broadcast Received by MyBroadcastSender.", Toast.LENGTH_SHORT).show();

int id = intent.getIntExtra("id", 1);

// send back to your class
Intent newIntent = new Intent();
newIntent.setAction(context.getString(R.string.broadcast_id));
newIntent.putExtra("id1", id);
context.sendBroadcast(newIntent);
context.sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
Toast.makeText(context, "Broadcast sent back.", Toast.LENGTH_SHORT).show();

}
}

这是 MyBroadcastReceiver.java 文件:

// BroadcastReceiver
public class MyBroadcastReceiver extends BroadcastReceiver {

public MyBroadcastReceiver(){
super();
}

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

int id2 = intent.getIntExtra("id1", 1);

if (intent.getAction() != null && intent.getAction().equals(getString(R.string.broadcast_id))) {

NotificationARBroadcastReceiver.mNotifyMgr.cancel(id2);

Intent intent1 = new Intent(MyService.this, MainActivity.class);
intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent1);

Toast.makeText(context, "Broadcast received by MyBroadcastReceiver. Now, you can perform actions.",
Toast.LENGTH_SHORT).show();

} else {
Toast.makeText(context, "Intent is null.", Toast.LENGTH_SHORT).show();
}
}
}

getNotificationNewRequestService() 中,我将通知 id 作为附加信息放在 “id” 中,然后在 MyBroadcastSender.java 中,我我得到这个额外的作为 int id = intent.getIntExtra("id", 1); 然后再次作为 newIntent.putExtra("id1", id);然后最终将它作为 int id2 = intent.getIntExtra("id1", 1);MyBroadcastReceiver.java 中获取,并尝试使用它作为 删除通知NotificationARBroadcastReceiver.mNotifyMgr.cancel(id2);.

抱歉代码太多,我必须全部上传,因为它们都是必需的。

我想要的是知道如何将相同的通知 id 从 NotificationARBroadcastReceiver.java(这是一个单独的 java 文件)传送到 MyBroadcastReceiver(这是 MyService.java 中的内部类)?

更新 1.0- 这是我打印出 mmMainid 值时发生的情况, id1:

D/m: 0
D/mMain: 9994
D/id: 0
D/id1: 0

最佳答案

假设 getNotificationService() == getNotificationNewRequestService() 看起来在构建和显示通知之前未调用 NotificationARBroadcastReceiver。

您最好在创建通知的地方生成通知 ID,然后将它添加到 Intent 中,您也不需要这样做。

因此从 NotificationARBroadcastReceiver.recieve() 调用 getNotificationNewRequestService() 并确保通知 ID 匹配。

关于android - 即使提供了通知 ID,也不会在单击操作按钮时删除通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41125004/

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