我正在发送一个通知,其中包含两个操作按钮,即"接受"和"拒绝"。
我正在关注这个 Github 存储库。
当用户单击"接受"时,将检查某些条件并相应地执行逻辑。
更新 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-这是我打印出m
、mMain
、id
、id1
的值时发生的情况:
D/m: 0
D/mMain: 9994
D/id: 0
D/id1: 0
假设getNotificationService() == getNotificationNewRequestService()
在构建和显示通知之前没有调用通知ARBroadcastReceiver。
您最好在创建通知的位置生成通知 ID,并将其添加到那里的意图中,也不需要这样做。
因此,从 NotificationARBroadcastReceiver.recieve() 调用 getNotificationNewRequestService() 并确保通知 ID 匹配。
编辑:
移动:
m = (new Random()).nextInt(10000);
以前:
actionIntent.putExtra("id", NotificationARBroadcastReceiver.m); // this will be 'm'
结果:
int m = (new Random()).nextInt(10000);
Intent actionIntent = new Intent(getBaseContext(), MyBroadcastSender.class);
actionIntent.putExtra("id", m);
Log.d(getClass().getSimpleName(), "Notification Id is : " + m);
然后,您可以检查id
、id1
和id2
中的值。不要忘记使用您从m
获得的相同ID呼叫.notify()
。
您还可以创建getRandomNotificationId()
和getLastGeneratedNotificationId()
方法。每当生成 Id 时,请将其存储在整数变量public static
以便可以在整个类中访问它。
问题可能是您在初始化之前从NotificationARBroadcastReceiver
访问m
。所以,肯定会0
.而且,您提到了有关println
错误的事情,您是否正在使用System.out.println()
?
编辑前:
如新编辑所示,请尝试在开始之前关闭通知:
m = (...);
// some code here
mNotifyMgr.cancel(m);
mNotifyMgr.notify(m, notification);
并查看您的问题是否已解决。