【问题标题】:Android: Transferring Data via ContentIntentAndroid:通过 ContentIntent 传输数据
【发布时间】:2015-06-08 08:53:21
【问题描述】:

我的 Android 应用程序有问题。它通过 Google Cloud Messaging 接收消息并在通知栏中显示通知。我想通过通知传输一些数据。此数据来自 Google Cloud Messaging,应附加到通知中。

这是我显示通知的代码:

private void setNotification(String strTitle, String strMessage, int intPageId, int intCategorieId)
    {
        notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);

        Intent intentOpen = new Intent(this, NotificationClick.class);
        intentOpen.putExtra("pageId", Integer.toString(intPageId));
        intentOpen.putExtra("categorieId", Integer.toString(intCategorieId));
        intentOpen.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intentOpen, 0);


        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.launcher_icon)
                .setTicker(strTitle)
                .setContentTitle(strTitle)
                .setStyle(new NotificationCompat.BigTextStyle().bigText(strMessage))
                .setContentText(strMessage);


        notificationBuilder.setContentIntent(contentIntent);
        notificationBuilder.setDefaults(Notification.DEFAULT_ALL);
        notificationManager.notify(intCategorieId, notificationBuilder.build());

    }

我的代码在另一个活动(通过单击通知打开女巫)中获取变量“intPageId”和“intCategorieId”,是这样的:

 Bundle intentExtras = new Bundle();
        intentExtras = getIntent().getExtras();
        Toast.makeText(this, "Page To Open: " + intentExtras.getString("pageId"), Toast.LENGTH_LONG).show();
        Toast.makeText(this, "Page To Open: " + intentExtras.getString("kategorieId"), Toast.LENGTH_LONG).show();

但是在两个“Toast”消息中都出现了“null”,或者是第一次通过的值,点击消息。

例如: 我第一次发送:intPageId=1。 Toast 说:“要打开的页面:1”。

第二次发送:intPageId=2。 Toast 说:“要打开的页面:1”。

我完全糊涂了。

【问题讨论】:

    标签: android push-notification google-cloud-messaging android-pendingintent


    【解决方案1】:

    第一种方法:

    而不是下面的行:

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intentOpen, 0);
    

    改成这样:

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intentOpen, PendingIntent.FLAG_UPDATE_CURRENT);
    

    第二种方法:

    int iUniqueId = (int) (System.currentTimeMillis() & 0xfffffff);
    
    PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(),iUniqueId, intentForNotification, 0);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-11-16
      • 2010-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多