woy*_*aru 5 android android-intent android-notifications android-activity
我的问题有点复杂,所以我将简要描述一下我想要实现的目标.我的应用程序从GCM任何问题接收消息.当应用程序正在运行并且对用户可见时,我没有在操作栏中显示通知,我在对话框警报中显示此消息.我BroadcastReceiver用来实现这一目标.
主要问题是,当应用程序立即收到一些通知时.当主要活动对用户可见时,第一个通知将显示在对话框警报中.接下来将像通常的Android通知一样放在Action Bar中.此时,用户会处理对话框警报,并将从操作栏中选择下一个通知.此刻我正在使用我的CloudService(延伸IntentService)intent旗帜Intent.FLAG_ACTIVITY_CLEAR_TOP和PendingIntent.FLAG_UPDATE_CURRENT旗帜pendingIntent.PendingIntent只是contentIntent为我的NotificationCompat.Builder.
它以这种方式工作,对于每个用户点击Action Bar的通知,我的活动正在刷新(浮动到较低的设备边缘,然后从上边缘浮动,带有消息的对话框警报 - 我从意图获取额外的onResume方法).这个动作很好.在这种情况下,Activity只有一个实例 - 在打开几个通知时,我不必突破同一活动的几个实例.但最大的问题是,当用户在每种情况下选择任何通知时,最后一个将在对话框警报中打开.intent尽管如此,活动似乎也是一样的PendingIntent.FLAG_UPDATE_CURRENT.
CloudService处理云消息有两种方法:
private void showNotification(Bundle extras) {
notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
String message = extras.getString(CloudMetaData.MESSAGE);
if (App.isMyActivityVisible() && !CloudMessageDialogFragment.isAttached()) {
sendBroadcast(message, NOTIFICATION_ID);
} else {
Intent intent = new Intent(this, MyParentActivity.class);
intent.putExtra(CloudMetaData.MESSAGE, message);
intent.putExtra(CloudMetaData.NOTIFICATION_ID, NOTIFICATION_ID);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setContentTitle(getString(R.string.app_name));
builder.setStyle(new NotificationCompat.BigTextStyle().bigText(alert));
builder.setContentText(alert);
builder.setAutoCancel(true);
builder.setDefaults(Notification.DEFAULT_SOUND);
builder.setContentIntent(contentIntent);
notificationManager.notify(NOTIFICATION_ID, builder.build());
}
NOTIFICATION_ID++;
}
Run Code Online (Sandbox Code Playgroud)
发送广播的方法就像那样:
private void sendBroadcast(String message, int notificationId) {
Intent intent = new Intent();
intent.setAction(ACTION_FROM_CLOUD_SERVICE);
intent.putExtra(CloudMetaData.MESSAGE, message);
intent.putExtra(CloudMetaData.NOTIFICATION_ID, notificationId);
sendBroadcast(intent);
}
Run Code Online (Sandbox Code Playgroud)
我可以通过哪种方式实现类似解决方案?但是,当然用户必须从Action Bar打开通知并向他显示正确的消息.
编辑
我已将对话框警报切换到对话框活动中.这是它的定义AndroidManifest:
<activity
android:name="com.myapp.activity.CloudMessageDialogActivity"
android:theme="@android:style/Theme.Dialog"
android:parentActivityName="com.myapp.activity.MyParentActivity"/>
Run Code Online (Sandbox Code Playgroud)
但结果仍然相同 - 当应用程序收到一些通知时,单击其中一个将打开对话框,其意图是最后收到的通知.
但最大的问题是,当用户在每种情况下选择任何通知时,最后一个通知将在对话框警报中打开。尽管 ,但 Activity 似乎具有相同的意图
PendingIntent.FLAG_UPDATE_CURRENT。
0这是因为您对此处的所有s使用相同的请求代码(即) PendingIntent:
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
Run Code Online (Sandbox Code Playgroud)
通过这样做,您最终会收到相同的意图,因为平台无法看到意图之间的差异并且每次都传递相同的对象:
...为了检索 PendingIntent 的目的,了解何时将两个 Intent 视为相同非常重要。人们常犯的一个错误是创建多个 PendingIntent 对象,其 Intent 仅在“额外”内容上有所不同,并期望每次都能获得不同的 PendingIntent。这不会发生。Intent 中用于匹配的部分与Intent.filterEquals定义的部分相同。如果您使用两个与 Intent.filterEquals 等效的 Intent 对象,那么您将为它们获得相同的 PendingIntent。
因此,请使用唯一的请求代码,而不是常量值。例如,将上面的行更改为:
PendingIntent contentIntent = PendingIntent.getActivity(this, NOTIFICATION_ID,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助。
| 归档时间: |
|
| 查看次数: |
3083 次 |
| 最近记录: |