Android 从google云消息接收器向活动传递意图附加值为空

Android 从google云消息接收器向活动传递意图附加值为空,android,android-intent,google-cloud-messaging,android-broadcast,Android,Android Intent,Google Cloud Messaging,Android Broadcast,我有一个广播接收器,可以监听云消息,我可以很好地显示通知,但当用户单击通知时,我希望将它们发送到适当的活动中,并提供额外的意图。附加值总是空的 我可以验证在调试时传递给挂起的intent的intent是否有额外的内容 我试图了解activites onCreate()方法中的意图 ***编辑添加工作代码,有几件事需要注意,如果任务已经启动,则不能指望调用oncreate()方法,因此获取额外内容的最佳位置似乎是onresume方法。我需要在意图和未决意图上设置标志 @EReceiver publ

我有一个广播接收器,可以监听云消息,我可以很好地显示通知,但当用户单击通知时,我希望将它们发送到适当的活动中,并提供额外的意图。附加值总是空的

我可以验证在调试时传递给挂起的intent的intent是否有额外的内容

我试图了解activites onCreate()方法中的意图

***编辑添加工作代码,有几件事需要注意,如果任务已经启动,则不能指望调用oncreate()方法,因此获取额外内容的最佳位置似乎是onresume方法。我需要在意图和未决意图上设置标志

@EReceiver
public class MyBroadcastReceiver extends BroadcastReceiver {

private static final String TAG = "googlecloudmessage";
private static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
private NotificationCompat.Builder builder;
private Context ctx;

@Pref
public MyPrefs_ myPrefs;

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

    Log.d(TAG, "received gcm message");

    GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
    this.ctx = context;

    String messageType = gcm.getMessageType(intent);

    if (messageType != null) {

        if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {

        }
        else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {

        }
        else if (messageType.equalsIgnoreCase("gcm")) {
            myPrefs.notification().put(true);
            handleNotification(intent);
        }
    }
    setResultCode(Activity.RESULT_OK);
}

private void handleNotification(Intent intent) {

    Bundle bundle = intent.getExtras();

    String typeString = bundle.getString("type");
    String icon = bundle.getString("icon");

    String title = null;
    String body = null;
    Class<?> intentClass = null;
    Integer intentExtraId = null;

    if (typeString == null)
        return;

    int type = Integer.parseInt(typeString);

    switch (type) {

    case 1:

        intentClass = FriendsActivity_.class;

        title = bundle.getString("username");

        break;

    case 2:

        intentClass = UserProfileActivity_.class;

        title = bundle.getString("username");

        intentExtraId = Integer.parseInt(bundle.getString("user_id"));

        break;




    }


    mNotificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);


Intent i = new Intent();
    i.setClass(ctx, intentClass);
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP     | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    i.putExtra("gcm", true);
    if (intentExtraId != null) {

        i.putExtra("id", intentExtraId);
    }

    int requestID = (int) System.currentTimeMillis();

    PendingIntent pendingIntent = PendingIntent.getActivity(ctx, requestID, i,     PendingIntent.FLAG_UPDATE_CURRENT);

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

    mBuilder.setSmallIcon(R.drawable.ic_stat_u);

    Bitmap bmp = null;

    try {
        bmp = Ion.with(ctx, ctx.getResources().getString(R.string.image_container) + icon).asBitmap().get();
    }
    catch (InterruptedException e) {

        e.printStackTrace();
    }
    catch (ExecutionException e) {

        e.printStackTrace();
    }

    if (icon != null) {
        mBuilder.setLargeIcon(bmp);
    }

    mBuilder.setContentTitle(title).setStyle(new NotificationCompat.BigTextStyle().bigText(body))
            .setContentText(body);

    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

}

你做了简单的通知。然后点击这个。在下面的示例中,将活动粘贴到Main活动的位置

public void createNotification(Context context, String payload, String message) {
    try {
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new Notification(R.drawable.ic_launcher, message, System.currentTimeMillis());
        // Hide the notification after its selected
        // notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notification.ledARGB = 0xff00ff00;
        notification.ledOnMS = 300;
        notification.ledOffMS = 1000;
        notification.flags |= Notification.FLAG_SHOW_LIGHTS;

        Intent intent = new Intent(context, YourMainActivity.class);
        intent.putExtra("payload", payload);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        intent.putExtra("NotifID", 1);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
        PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        notification.setLatestEventInfo(context, "Message", message, pendingIntent);
        notification.defaults |= Notification.DEFAULT_SOUND;
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        notificationManager.notify(0, notification);
    } catch (Exception e) {

            Log.e(this.getClass().getSimpleName(), e.getMessage(), e);

    }
}

如果有帮助,请标记为true或向上投票这是您用来获取挂起内容的呼叫:

PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0, i, 0);
如果系统中已经有一个匹配的
pendingent
,此调用将只返回该内容。它可能包含也可能不包含您想要的额外内容。要确保使用额外的资源,您必须确保更新任何当前的
pendingent
,如下所示:

PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);

你可以试试这个。它是:

/////////添加此方法

private void sendNotification(String message) {
    Intent intent = new Intent(this, ListLocations.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_park_notification)
            .setContentTitle("Parkuest Message")
            .setContentText(message)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}

您好,大卫,谢谢您的回复,将该标志添加到待决意向中确实会发送额外信息,但我只能在onresume或onstart中获取这些信息,但不能在通常获取额外信息的地方创建这些信息,这似乎很奇怪。知道为什么会发生这种情况吗?发布您的清单,我会看一看。同时在
onCreate()中发布用于获取额外信息的代码
@Override
public void onMessageReceived(String from, Bundle data) {
    String message = data.getString("message");
    Log.d(TAG, "From: " + from);
    Log.d(TAG, "Message: " + message);

    if (from.startsWith("/topics/")) {
        // message received from some topic.
        sendNotification(message)
    } else {
        // normal downstream message.
    }

    // ...
}
private void sendNotification(String message) {
    Intent intent = new Intent(this, ListLocations.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_park_notification)
            .setContentTitle("Parkuest Message")
            .setContentText(message)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}