当用户点击推送通知消息时,我的应用程序将启动。如何让应用程序完整显示推送消息?

时间:2015-07-23 09:04:23

标签: java android android-intent android-studio push-notification

美好的一天!

我已成功将推送通知消息发送到我在Android上的应用程序,当我点击该消息时,它会启动我的应用程序。请问如何从推送通知中将intent / bundle传递给MainActivity,以便在启动应用程序时,它可以在应用程序中完整显示推送通知消息?非常感谢你!

public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

    // Explicitly specify that GcmMessageHandler will handle the intent.
    ComponentName comp = new ComponentName(context.getPackageName(),
            GcmMessageHandler.class.getName());

    showNotification(context, intent);

    // Start the service, keeping the device awake while it is launching.
    startWakefulService(context, (intent.setComponent(comp)));
    setResultCode(Activity.RESULT_OK);
}

private void showNotification(Context context, Intent intent) {
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
            new Intent(context, MainActivity.class), 0);

    String title = intent.getExtras().getString("nTitle");
    String message = intent.getExtras().getString("nMessage");

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
            context);

    Notification notification = mBuilder.setContentIntent(contentIntent)
            .setSmallIcon(R.drawable.face)
            .setColor(context.getResources().getColor(R.color.wallet_holo_blue_light))
            .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.fuckya))
            .setWhen(0)
            .setAutoCancel(true)
            .setContentTitle(title)
            .setStyle(new NotificationCompat.BigTextStyle().bigText(message))
            .setContentText(message).build();

    mBuilder.setContentIntent(contentIntent);
    mBuilder.setDefaults(Notification.DEFAULT_SOUND);
    mBuilder.setAutoCancel(true);
    NotificationManager mNotificationManager =
            (NotificationManager)     context.getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(1, mBuilder.build());
}

}

此致 Lorkh

1 个答案:

答案 0 :(得分:0)

替换为:

Intent mainActivityIntent = new Intent(context, MainActivity.class);
String title = intent.getExtras().getString("nTitle");
String message = intent.getExtras().getString("nMessage");
mainActivityIntent.putExtra("nTitle", title);
mainActivityIntent.putExtra("nMessage",message);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, mainActivityIntent), 0);

并通过getIntent();

在您的MainActivity中检索此值