Android:通过ContentIntent传输数据 [英] Android: Transferring Data via ContentIntent
问题描述
我的Android应用程序出现问题.它通过Google Cloud Messaging接收消息,并在通知栏中显示通知.我想通过通知传输一些数据.此数据来自Google Cloud Messaging,应附加到通知中.
I've got a problem with my Android Application. It receives Messages via Google Cloud Messaging and displays a Notification in the Notificationbar. I want to transfer some Data with the Notification. This Data comes via Google Cloud Messaging and should be attached to the Notification.
这是我显示通知的代码:
This is my Code to Display the Notification:
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());
}
然后我的代码在另一个Activity(通过单击通知打开女巫打开)中获取变量"intPageId"和"intCategorieId",是这样的:
And my Code to get the Variables "intPageId" and "intCategorieId" in a other Activity(witch opens by clicking the Notification) back, is this:
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",或者是第一次传递的值,请单击"Message".
But in both "Toast" Messages appears "null", or the value that was passed through at the first time, clicking on the Message.
例如: 第一次发送:intPageId = 1. 吐司面包说:要打开的页面:1".
For Example: At the First time I send: intPageId=1. The Toast says: "Page To Open: 1".
第二次我发送:intPageId = 2. 吐司面包说:要打开的页面:1".
At the Second time I send: intPageId=2. The Toast says: "Page To Open: 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);
这篇关于Android:通过ContentIntent传输数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!