【发布时间】:2012-07-30 09:02:46
【问题描述】:
我正在处理推送通知。当前正在接收和显示通知,但点击事件未按应有的方式运行。
int NOTIFICATION_ID = 1593;
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.notification, title, System.currentTimeMillis());
notification.flags |= Notification.FLAG_AUTO_CANCEL;
Intent notificationIntent = new Intent(this, MyMainActivity.class);
notificationIntent.putExtra("test", "test");
notificationIntent.setAction("load_notifications");
PendingIntent intentNotification = PendingIntent.getActivity(this, 999, notificationIntent, 0);
notification.setLatestEventInfo(this, title, body, intentNotification);
notificationManager.notify(NOTIFICATION_ID, notification);
这就是我显示通知的方式,它还包括开始/恢复我的主要活动。
但是,如果我在应用程序中单击通知,putExtra("test", "test") 和 setAction 似乎无效。如果我的应用无法运行,从通知中启动它可以正常工作。
我的主要活动中的 onResume 函数:
public void onResume() {
super.onResume();
if(this.getIntent().getAction().equalsIgnoreCase("load_notifications")) {
//Do stuff
}
}
我的应用配置如下:
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".Activity"
android:label="@string/app_name"
android:configChanges="orientation"
android:launchMode="singleInstance"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Push notifications -->
<receiver android:name="com.google.android.gcm.GCMBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.app.android" />
</intent-filter>
</receiver>
<service android:name=".GCMIntentService" />
</application>
【问题讨论】:
-
我会尝试输入一些日志输出,以检查它是否根本没有进入那里,或者它是否只是没有进入你的 if 循环。也许将动作加载到变量并打印出来?
-
我删除了代码块的日志调用。活动保持为主,而不是新设置的“load_notifications”,我添加的测试变量只是空。
标签: android android-activity android-notifications