【问题标题】:Activity does not get correct bundle活动没有得到正确的捆绑
【发布时间】:2015-04-17 13:44:29
【问题描述】:

我正在使用 GCM,这是我的 onMessage 函数:

@Override
protected void onMessage(Context context, Intent intent) {
    if (intent != null && intent.getExtras() != null) {
        try {
            String message = intent.getExtras().getString("message");
            String userId = intent.getExtras().getString("user_id");
            Log.i("","postda user id is:" + userId);
            Log.i("","will enter here FRIENDS: ");
            generateNotification(context, message, userId);
        } catch (Exception e) {
            Utils.appendLog("onMessage errror : " + e.getMessage());
            Log.i(TAG, e.getMessage(), e);
        }
    }
}

这是我的 CreateNotification 函数:

private static void generateNotification(Context context, String message, String userID) {
    Log.i("", "postda user message " + message + ".... userid: " + userID);
    String title = context.getString(R.string.passenger_name);
    Intent notificationIntent = new Intent(context, PSProfileActivity.class);
    notificationIntent.putExtra("id", userID);
    Log.i("", "postda ---------- userid: "+ userID);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
    Log.i("", "postda message: " + message + "....userID " + userID );

    PSLocationCenter.getInstance().pref.setDataChanged(context, true);
    PSLocationCenter.getInstance().pref.setDataChangedProfile(context, true);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
    mBuilder.setContentTitle(title).setContentText(message).setSmallIcon(R.drawable.notification_icon);
    mBuilder.setContentIntent(intent);
    Notification notification = mBuilder.build();
    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(1, notification);

    playTone(context);
}

这是 PSProfileActivity onCreate 函数:

@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_profile);
    ButterKnife.inject(this);

    mHeader = new ProfileHeader(this, backFromHeader);
    mData = new ProfileData(this);
    mButtons = new ProfileViewPagerButtons(PSProfileActivity.this, firstPage);

    Bundle bundle = getIntent().getExtras();
    if(bundle != null) id = bundle.getString("id");
    Log.i("", "postda in profile id is:" + id);
    if(!id.contentEquals(String.valueOf(PSLocationCenter.getInstance().pref.getUserId(PSProfileActivity.this)))){
        findViewById(R.id.action_settings).setVisibility(View.INVISIBLE);
    }
    Log.i("", "postda in profile id is 2:" + id);
}

这是我的 Logcat 回复:

04-17 15:33:53.650   9699-11695/nl.hgrams.passenger I/﹕ postda user id is:23
04-17 15:33:53.651   9699-11695/nl.hgrams.passenger I/﹕ postda user message alin reddd accepted your friend request..... userid: 23
04-17 15:33:53.651   9699-11695/nl.hgrams.passenger I/﹕ postda ---------- userid: 23
04-17 15:33:53.652   9699-11695/nl.hgrams.passenger I/﹕ postda message: alin reddd accepted your friend request.....userID 23
04-17 15:33:58.812    9699-9699/nl.hgrams.passenger I/﹕ postda in profile id is:28
04-17 15:33:58.814    9699-9699/nl.hgrams.passenger I/﹕ postda in profile id is 2:28

如您所见,我通过捆绑包发送了一个 ID,但我在另一页上得到的是一个完全不同的 ID(它实际上是应该在这里获得的最后一个 ID)。这真的很奇怪,有人知道为什么会这样吗?

【问题讨论】:

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


    【解决方案1】:

    PendingIntent 中使用唯一标识符:

    int iUniqueId = (int) (System.currentTimeMillis() & 0xfffffff);      
    PendingIntent.getActivity(context, iUniqueId, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    

    【讨论】:

    • 试过这样,但我仍然遇到同样的问题,即使我有一个 uniqueID
    【解决方案2】:

    您总是使用相同的 id 生成意图。您需要将唯一标识符作为第二个参数传递,并且还需要添加标志 FLAG_UPDATE_CURRENT 以便更新附加信息,并且您始终获得您传递的最新附加信息。

    所以你必须像这样生成意图:

    PendingIntent intent = PendingIntent.getActivity(context, identifier , notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT );
    

    标识符可以是任何独特的东西,例如自动增量变量或当前时间戳

    【讨论】:

      【解决方案3】:

      问题可能是您只使用 extras 来修改 PendingIntent。尝试将 id 嵌入到 notificationIntent 的数据字段中。

      来自http://developer.android.com/reference/android/app/PendingIntent.html

      人们犯的一个常见错误是创建多个 PendingIntent 对象,其 Intent 仅在其“额外”内容上有所不同,期望每次都获得不同的 PendingIntent。这不会发生。 Intent 中用于匹配的部分与 Intent.filterEquals 定义的部分相同。如果您使用两个根据 Intent.filterEquals 等效的 Intent 对象,那么您将获得相同的 PendingIntent。

      【讨论】:

        【解决方案4】:

        我认为你必须使用这个标志,FLAG_UPDATE_CURRENT

        像这样: PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        这将使用您通过捆绑传递的新附加内容刷新旧意图,并获取与 ID 匹配的正确数据。

        【讨论】:

          猜你喜欢
          • 2020-02-28
          • 2019-07-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-01-03
          • 2021-08-08
          • 2017-10-18
          相关资源
          最近更新 更多