安卓:通过ContentIntent传输数据 [英] Android: Transfering Data via ContentIntent

查看:156
本文介绍了安卓:通过ContentIntent传输数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的Andr​​oid应用程序有问题。它通过谷歌云消息接收消息并显示在Notificationbar的通知。我想用传送通知一些数据。该数据来自通过谷歌云消息,应attatched的通知。

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 attatched to the Notification.

这是我的code显示通知:

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());

    }

和我的code来获得在其他活动中的可变因素intPageId和intCategorieId(巫婆通过单击通知打开)回来了,是这样的:

And my Code to get the Varibles "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();

但在这两个吐司的消息出现空,或通过在第一时间传递的值,点击该消息。

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".

我完全糊涂了,希望有人能帮助我。

I'm totally confused and hope someone can help me.

(对不起,spelling-和语法错误)

(Sorry for spelling- and grammatical errors)

推荐答案

第一种方法:

而不是下面一行:

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);

这篇关于安卓:通过ContentIntent传输数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆