テストサーバーを介して送信する通知の内容を表示するテキストボックスを備えたアプリがあります。アプリがフォアグラウンドまたはバックグラウンドにある場合 (ホームキーを押した場合)、アプリは正常に動作します。
私が使用している GCM デモ コードでは、通知をタップすると、アプリが再びフォーカスされ、テキスト ボックスに正しく入力されます。
戻るキーを押すか、メニューからアプリを終了しても、引き続き通知を受け取ることができます。しかし、通知をクリックすると、アプリが開き、テキストボックスは空白のままになります。
アプリを停止すると、onDestroy() メソッドが本質的にすべてを強制終了することを知っています。私のハンドラーなどはすべてなくなりました。では、アプリの停止後に通知内容を表示するにはどうすればよいでしょうか?
GCMIntentService.java から (これは定型コードで、ここでは何も変更されていません):
/**
* Issues a notification to inform the user that server has sent a message.
*/
@SuppressWarnings("deprecation")
private static void generateNotification(Context context, String message) {
int icon = R.drawable.ic_stat_gcm;
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, DemoActivity.class);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent =
PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
}
テキストボックスを設定するハンドラー (これは MainActivity にあります):
private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String newMessage = intent.getExtras().getString(EXTRA_MESSAGE);
mDisplay.append(newMessage + "\n");
String regid = intent.getExtras().getString("regid");
regIdTxt.setText(regid);
}
};
メッセージの登録、登録解除、および受信ができるので、問題がなければマイ マニフェスト。