0

当我收到通知并单击下拉栏中的选项卡时,消息活动正在启动但不显示收到的消息。

在消息活动处于活动状态时,下一条消息显示良好。

我怎样才能显示第一条/打开消息?

谢谢。

GCMIntentService.java(部分)

private static void generateNotification(Context context, String message) {
        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, MessageActivity.class);
        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;

        // Play default notification sound
        notification.defaults |= Notification.DEFAULT_SOUND;

        // Vibrate if vibrate is enabled
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        notificationManager.notify(0, notification);      

    }

MessageActivity.class

public class MessageActivity extends Activity {
    // label to display gcm messages
    TextView lblMessage;

    // Alert dialog manager
    AlertDialogManager alert = new AlertDialogManager();

    // Connection detector
    ConnectionDetector cd;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message);

        cd = new ConnectionDetector(getApplicationContext());

        // Check if Internet present
        if (!cd.isConnectingToInternet()) {
            // Internet Connection is not present
            alert.showAlertDialog(MessageActivity.this,
                    "Internet Connection Error",
                    "U heeft geen verbinding met internet!", false);
            // stop executing code by return
            return;
        }

        lblMessage = (TextView) findViewById(R.id.lblMessage);

        registerReceiver(mHandleMessageReceiver, new IntentFilter(
                DISPLAY_MESSAGE_ACTION));
    }       

    /**
     * Receiving push messages
     * */
    private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String newMessage = intent.getExtras().getString(EXTRA_MESSAGE);
            // Waking up mobile if it is sleeping
            WakeLocker.acquire(getApplicationContext());
            // Showing received message
            lblMessage.append(newMessage + "\n");           
            Toast.makeText(getApplicationContext(), "Nieuw bericht: " + newMessage, Toast.LENGTH_LONG).show();

            // Releasing wake lock
            WakeLocker.release();
        }
    };
}
4

2 回答 2

3

好吧,我是这样解决的:

在 GCMIntentService.java 中添加

notificationIntent.putExtra("message", message);
PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT );

并在 MessageActivity.class

TextView message= (TextView) findViewById(R.id.lblMessage);
message.setText(getIntent().getExtras().getString("message"));
于 2012-11-13T11:42:12.960 回答
0

BroadcastReceiver仅在MessageActivity运行时注册。您需要在清单中注册它,以便它在MessageActivity不运行时工作。

您需要扩展 IIRCBroadcastReceiver才能执行此操作。

于 2012-11-12T19:49:26.087 回答