问题描述
我正在尝试在点击通知操作时运行重试例程,为此,我创建了一个 BroadcastReceiver
并将其注册到 AndroidManifest
文件中。
在创建带有操作的通知时,我正在使用 PendingIntent
并使用通知操作设置该待处理意图。
当应用程序正在运行或在后台(未从最近的应用程序列表中删除)时,点击通知操作后会立即收到广播接收器。但是在杀死应用程序(从最近的应用程序列表中将其删除)后,广播接收器需要一些触发。
以下是不同组件的代码片段。
AndroidManifest 文件
<receiver android:name=".Receiver.RetryStatusBroadcastReceiver" android:enabled="true" android:exported="false"/>
广播接收器
public class RetryStatusBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context,Intent intent) {
}
}
通知触发代码
Intent retryIntent = new Intent(this,RetryStatusBroadcastReceiver.class);
PendingIntent retryPendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(),uniqueId + 1,retryIntent,0);
NotificationCompat.Action retryAction = new NotificationCompat.Action(R.drawable.ic_retry,"Retry",retryPendingIntent);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this,getString(R.string.background_syncing_notification_channel_id))
.setSmallIcon(R.drawable.app_logo)
.setPriority(Notification.PRIORITY_HIGH)
.setDefaults(Notification.DEFAULT_ALL)
.setContentTitle(notificationTitle)
.setContentText(notificationMessage)
.setStyle(new NotificationCompat.BigTextStyle().bigText(notificationMessage))
.setAutoCancel(true)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.addAction(retryAction);
Intent intent = new Intent(this,HomeActivity.class);
PendingIntent activityPendingIntent = PendingIntent.getActivity(this,uniqueId,intent,PendingIntent.FLAG_ONE_SHOT);
notificationBuilder.setContentIntent(activityPendingIntent);
notificationBuilder.setAutoCancel(true);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (notificationManager != null) {
notificationManager.notify(uniqueId,notificationBuilder.build());
}
我也尝试从 WakefulBroadcastReceiver
扩展我的 BroadcastReceiver,但结果是一样的。
我也尝试删除 android:enabled
和 android:exported
标签,但这里似乎没有任何效果。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)