【发布时间】:2019-04-11 11:49:38
【问题描述】:
我目前正在开发一个接收推送通知的 Android 应用程序。现在我显示的通知通常有一个操作按钮,它应该执行如下代码 sn-p 所示的操作:
public class PushReceiver extends BroadcastReceiver implements RequestCallbacks {
private RequestHandler requestHandlerInstance;
private SessionHandler sessionHandlerInstance;
private Context context;
@Override
public void onReceive(Context context, Intent intent) {
this.context = context.getApplicationContext();
requestHandlerInstance = RequestHandler.getInstance(context);
sessionHandlerInstance = SessionHandler.getInstance(context);
String id = intent.getStringExtra("error_id");
requestHandlerInstance.startRequest(new RequestOperation(RequestOperation.Type.ERROR_TAKE_OVER, sessionHandlerInstance.getDeviceHash(), id), this);
}
#these are callbacks that will be executed
#when starRequest() returns a response
@Override
public void onSuccess(JSONObject json, String parsingKey) {
#request was successful
}
@Override
public void onError(VolleyError error, String parsingKey) {
#request failed, activating panic mode
}
@Override
public void onFinished(String parsingKey) {
#here i plan to dismiss the notification box,
#but this doesn't seem to be the correct approach
#as the box is still there
Intent intent = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
context.sendBroadcast(intent);
}
}
有没有人有适当的解决方案如何关闭通知框?当前不需要 onClick 事件的 UI 响应。或者我是否必须从我的 MessagingService 发送一个新的 Intent,它会覆盖以前的通知?
为了更清楚,这是我创建通知的方式:
Notification notification;
Intent activityIntent = new Intent(this.getApplicationContext(), MainActivity.class);
activityIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(this.getApplicationContext(), REQUEST_CODE, activityIntent, PendingIntent.FLAG_ONE_SHOT);
Intent broadcastIntent = new Intent(this, PushReceiver.class);
broadcastIntent.putExtra("error_id", remoteMessage.getData().get("id"));
PendingIntent actionIntent = PendingIntent.getBroadcast(this, REQUEST_CODE, broadcastIntent, PendingIntent.FLAG_ONE_SHOT);
notification = new NotificationCompat.Builder(this, getString(R.string.default_notification_channel_id))
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(remoteMessage.getData().get("notification_title"))
.setContentText(remoteMessage.getData().get("notification_text"))
.setSound(settingsHandlerInstance.getRingtoneUri())
.setVibrate(settingsHandlerInstance.shouldVibrateOnPush() ? new long[] {0, 500, 200, 500, 0} : new long[] {0, 0, 0, 0, 0 })
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
.setContentIntent(contentIntent)
.addAction(R.mipmap.ic_launcher, "Übernehmen", actionIntent)
.setAutoCancel(true)
.build();
【问题讨论】:
-
您可以在 NotificationManager 对象上调用“cancel(int id)”。但是,您必须跟踪通知 ID。
-
可以这么说,我可以将 notification_id 作为额外的意图传递并在 pushReceiver 中调用 cancel() 吗?
-
是的,试试看它是否适合你。确保您可以从 pushReceiver 访问 NotificationManager 对象
-
这可能是一个警告,通知管理器是我的 MessagingService 的一个属性。它必须是同一个对象吗?
-
有什么方法可以通过意图将 NotificationManagerCompat 传递给我的 BroadcastReceiver?
标签: java android broadcastreceiver android-notifications