在我们的应用程序OneBusAway Android(Github上的开源)中,当用户关闭特定的提醒通知时,我们需要得到通知,因此我们不会针对同一事件发布另一条提醒通知(直到他们的公交车到达之前需要多长时间)。
我们通过侦听一个这样Intent
在我们的应用程序,注册为DeleteIntent
用Notification
。当用户关闭通知时(通过将其擦掉或点击通知窗口中的清除按钮),我们的应用应会收到该通知Intent
。
通过测试,似乎在Google Play上的当前版本(以及Github上的当前master分支)中,以下版本的Android中从未在我们的应用程序中收到DeleteIntent:
然而,确切的相同的代码DOES工作(即,登记为DeleteIntent意图是由该应用接收到的)上:
我看过以下有关DeleteIntent的SO帖子,列出的所有解决方案均不适用于Android 4.4.3和4.4.4:
当前工作的master分支使用Service侦听Intent。但是,基于上述一些文章,我确实对一些代码进行了调整,以使其更符合使用BroadcastReceiver侦听Intent的工作示例。
使用BroadcastReceiver的代码在以下Github分支中:
https://github.com/CUTR-at-USF/onebusaway-android/tree/issue104-RepeatingReminders
以下是我当前版本外观的摘录(仍适用于Android 4.1.2及更低版本,但不适用于4.4.3或4.4.4),以及指向Github来源的链接:
创建通知
private Notification createNotification(Uri alertUri) {
//Log.d(TAG, "Creating notification for alert: " + alertUri);
Intent deleteIntent = new Intent(mContext, AlarmReceiver.class);
deleteIntent.setAction(TripService.ACTION_CANCEL);
deleteIntent.setData(alertUri);
return new NotificationCompat.Builder(mContext)
.setSmallIcon(R.drawable.ic_stat_notification)
.setDefaults(Notification.DEFAULT_ALL)
.setOnlyAlertOnce(true)
.setDeleteIntent(PendingIntent.getBroadcast(mContext, 0,
deleteIntent, PendingIntent.FLAG_UPDATE_CURRENT))
.setAutoCancel(true)
.build();
}
标题和其他动态通知信息会在几行后设置(如果通知仍未取消,则稍后会重置):
@SuppressWarnings("deprecation")
private void setLatestInfo(Notification notification,
String stopId,
String routeId,
long timeDiff) {
final String title = mContext.getString(R.string.app_name);
final PendingIntent intent = PendingIntent.getActivity(mContext, 0,
new ArrivalsListActivity.Builder(mContext, stopId).getIntent(),
PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(mContext,
title,
getNotifyText(routeId, timeDiff),
intent);
}
TripService
包含操作的常量:
public static final String ACTION_CANCEL =
"com.joulespersecond.seattlebusbot.action.CANCEL";
报警接收器
public class AlarmReceiver extends BroadcastReceiver {
private static final String TAG = "AlarmReceiver";
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "In onReceive with intent action " + intent.getAction());
...
}
}
Android清单
<receiver android:name=".AlarmReceiver">
<!-- These action names must match the constants in TripService -->
<intent-filter>
<action android:name="com.joulespersecond.seattlebusbot.action.SCHEDULE" />
<action android:name="com.joulespersecond.seattlebusbot.action.POLL" />
<action android:name="com.joulespersecond.seattlebusbot.action.CANCEL" />
</intent-filter>
</receiver>
通过以上操作,在Android 4.4.3 / 4.4.4上,当用户关闭通知时,AlarmReceiver永远不会看到Intent。
我还尝试添加MIME类型,如在“自定义动作”中使用应用程序之间的隐式意图指定的那样,但这在Android 4.4.3 / 4.4.4上也不起作用:
Intent deleteIntent = new Intent(mContext, AlarmReceiver.class);
deleteIntent.setAction(TripService.ACTION_CANCEL);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
deleteIntent.setDataAndTypeAndNormalize(alertUri, TripService.REMINDER_MIME_TYPE);
} else {
deleteIntent.setDataAndType(alertUri, TripService.REMINDER_MIME_TYPE);
}
return new NotificationCompat.Builder(mContext)
.setSmallIcon(R.drawable.ic_stat_notification)
.setDefaults(Notification.DEFAULT_ALL)
.setOnlyAlertOnce(true)
.setDeleteIntent(PendingIntent.getBroadcast(mContext, 0,
deleteIntent, 0))
//.setLights(0xFF00FF00, 1000, 1000)
//.setVibrate(VIBRATE_PATTERN)
.build();
REMINDER_MIME_TYPE
是 application/vnd.com.joulespersecond.seattlebusbot.reminder
使用MIME类型的清单:
<receiver android:name=".AlarmReceiver">
<!-- These action names must match the constants in TripService -->
<intent-filter>
<action android:name="com.joulespersecond.seattlebusbot.action.SCHEDULE" />
<action android:name="com.joulespersecond.seattlebusbot.action.POLL" />
<action android:name="com.joulespersecond.seattlebusbot.action.CANCEL" />
<data android:mimeType="application/vnd.com.joulespersecond.seattlebusbot.reminder" />
</intent-filter>
</receiver>
我也尝试过不使用支持库(即,使用Notification.Builder
而不是NotificationCompat.Builder
),但这也没有任何改变。
有什么想法为什么在Android 4.4.3 / 4.4.4上不起作用?
Github问题中显示了有关此问题的更多信息。
编辑
我还在一个小型Github项目“ DeleteIntentDemo”中复制了此问题:
https://github.com/barbeau/DeleteIntentDemo
复制说明在该项目的自述文件中。
编辑2
这似乎是由于Android中的错误所致Notification.setLatestEventInfo()
-我在这里进行了举报:https://code.google.com/p/android/issues/detail?id = 73720
解决方法请参阅@CommonsWare的答案。
编辑3
我的AOSP补丁已解决此问题,现已合并,因此在以后的Android版本中,旧应用程序不会出现此问题:https : //code.google.com/p/android/issues/detail? id=73720#c4
但是,在上面的AOSP线程中强调的是,不应再使用Notification.setLatestEventInfo()
-而是使用它Notification.Builder
来创建新的Notification。
在示例项目中,如果删除以下行,则deleteIntent
可以在运行4.4.4的Nexus 4上运行:
setLatestInfo(getActivity(), notification, routeId);
我怀疑此电话抹去了您的电话deleteIntent
。它可以工作,你重新申请deleteIntent
到Notification
为你的一部分setLatestInfo()
处理。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句