启动活动中不显示通知消息

时间:2012-11-12 19:34:39

标签: android android-intent google-cloud-messaging

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

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

如何显示第一条/开场信息?

感谢。

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();
        }
    };
}

2 个答案:

答案 0 :(得分: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"));

答案 1 :(得分:0)

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

IIRC你需要延长BroadcastReceiver才能做到这一点。