0

我有一个带有文本框的应用程序,它显示我通过测试服务器发送的通知内容。该应用程序工作正常,当它在前台或后台时(当我按下主页键时)。

一旦我点击通知,我正在使用的 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);
    }
};

我的清单如果没问题,因为我可以注册、取消注册和接收消息。

4

0 回答 0