当点击通知时更新活动
问题描述:
我在Android中使用推送通知。当我收到一个推送通知并点击它时,它会根据我的需要启动一项活动,但如果此活动已打开,则此活动将保留旧数据。当点击通知时更新活动
AndroidManifest.xml中
<activity
android:name=".Detail"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="@string/title_activity_detail"
android:launchMode="singleTop"
android:parentActivityName=".Home"
android:theme="@style/Theme.AppCompat.Light.DarkActionBar" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="ma.via.marocagenda.Home" />
</activity>
GCMIntentService.java
public class GCMIntentService extends GCMBaseIntentService {
...
private static void generateNotification(Context context, String message) {
//int icon = R.drawable.abc_ic_go;
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context, Detail.class);
// put extra for details
notificationIntent.putExtra("id", VAL_TAG_ID);
notificationIntent.putExtra("titre", VAL_TAG_TITRE);
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.DEFAULT_LIGHTS;
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
//PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
}
什么可以是错误的?
答
无论何时推送通知到达,您都可以使用本地广播。
关于如何使用本地广播读到这里https://stackoverflow.com/a/8875292/826657
让这个广播动作你的活动注册,并在收到推送通知时,从onReceive()
更新活动。
此外,更具体的使用ActivityManager,你可以检查,如果你的活动是顶部,如果真的发送广播也,否则只显示通知。
在这里阅读如何找到当前的前景活动。 https://stackoverflow.com/a/4753333/826657
+0
作为您可以在我的代码中看到,在generateNotification方法中,我希望仅在单击通知时启动** detail **活动,而不是在收到它时 – leonidas79 2014-08-31 12:21:22
你的问题是什么? – suitianshi 2014-08-30 13:28:32
如果“详细信息”活动已启动,并且点击通知没有任何事情发生,活动不会用新数据更新 – leonidas79 2014-08-30 13:29:31
也覆盖onNewIntent? – Deucalion 2014-08-30 13:42:15