我发送的通知中有2个操作按钮,即#34;接受"和"拒绝"。
我跟随此Github repo。
当用户点击"接受"时,会检查某些条件并相应地执行逻辑。
更新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);
,最后将其作为MyBroadcastReceiver.java
添加到int id2 = intent.getIntExtra("id1", 1);
并尝试使用NotificationARBroadcastReceiver.mNotifyMgr.cancel(id2);
删除通知。
对于这么多代码感到抱歉,我必须将它们全部上传,因为它们都是必需的。
我想要的是知道如何将NotificationARBroadcastReceiver.java
(这是一个单独的java文件)中的相同通知ID传递给MyBroadcastReceiver
(这是一个内部类) MyService.java
)?
更新1.0 - 这是我打印出m
,mMain
,id
,id1
的值时发生的事情:
D/m: 0
D/mMain: 9994
D/id: 0
D/id1: 0
答案 0 :(得分:2)
假设getNotificationService() == getNotificationNewRequestService()
看起来在构建和显示notfication之前未调用NotificationARBroadcastReceiver。
您最好在您创建通知的位置生成通知ID,并将其添加到意图中,您也不需要这样做。
所以从NotificationARBroadcastReceiver.recieve()调用getNotificationNewRequestService()并确保通知ID匹配。
答案 1 :(得分:2)
修改:
举:
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
中的值。不要忘记使用.notify()
中的相同ID来呼叫m
。
您还可以创建getRandomNotificationId()
和getLastGeneratedNotificationId()
方法。无论何时生成Id,都将其存储在public static
整数变量中,以便您可以在整个班级中访问它。
问题可能是您在初始化之前从m
访问NotificationARBroadcastReceiver
。所以,肯定是0
。并且,您提到了println
错误,您使用的是System.out.println()
吗?
编辑前:
如新编辑所示,请在启动前尝试关闭通知:
m = (...);
// some code here
mNotifyMgr.cancel(m);
mNotifyMgr.notify(m, notification);
并查看您的问题是否得到解决。