android– 为什么我的Activity总是从GCM通知中显示相同的数据集?

android– 为什么我的Activity总是从GCM通知中显示相同的数据集?,第1张

概述我已经实现了一个简单的GCM客户端,我在更新每次收到新通知时启动的Activity时遇到此问题.每次我发送带有一组参数的通知时,只有我为第一个通知发送的参数才会出现在UI中.当我尝试更改参数时,它们不会显示(再次显示相同的数据).这是我的GcmIntentService类publicclassGcmIntent

我已经实现了一个简单的GCM客户端,我在更新每次收到新通知时启动的Activity时遇到此问题.每次我发送带有一组参数的通知时,只有我为第一个通知发送的参数才会出现在UI中.当我尝试更改参数时,它们不会显示(再次显示相同的数据).

这是我的GcmIntentService类

public class GcmIntentService extends IntentService {private static final String TAG = "GcmIntentService";public static final int NOTIFICATION_ID = 1;private notificationmanager mnotificationmanager;NotificationCompat.Builder builder;public GcmIntentService() {    super("GcmIntentService");}@OverrIDeprotected voID onHandleIntent(Intent intent) {    Bundle extras = intent.getExtras();    GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);    String messageType = gcm.getMessageType(intent);    if (!extras.isEmpty()) {        if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR                .equals(messageType)) {            Log.d(TAG, "GCM error -> MESSAGE_TYPE_SEND_ERROR");            sendNotification("Send error: " + extras.toString(), "", "",                    "", "");        } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED                .equals(messageType)) {            Log.d(TAG, "GCM error -> MESSAGE_TYPE_DELETED");            sendNotification(                    "Deleted Messages on server: " + extras.toString(), "",                    "", "", "");        } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE                .equals(messageType)) {            Log.d(TAG,                    "GCM success. Received data: fromUser="                            + extras.getString("fromUser") + " message="                            + extras.getString("message") + " srcLat="                            + extras.getString("srcLat") +" srcLng="                            + extras.getString("srcLng") + " dstLat="                            + extras.getString("dstLat") + " dstLng="                            + extras.getString("dstLng"));            sendNotification(                    extras.getString("fromUser") + " "                            + extras.getString("message"),                            extras.getString("srcLat"), extras.getString("srcLng"),                            extras.getString("dstLat"), extras.getString("dstLng"));        }    }    GcmbroadcastReceiver.completeWakefulintent(intent);}private voID sendNotification(String msg, String srcLat, String srcLng,        String dstLat, String dstLng) {    mnotificationmanager = (notificationmanager) this            .getSystemService(Context.NOTIFICATION_SERVICE);    Intent intent = new Intent(this, ShowUser.class);    intent.putExtra("message", msg);    intent.putExtra("srcLat", srcLat);    intent.putExtra("srcLng", srcLng);    intent.putExtra("dstLat", dstLat);    intent.putExtra("dstLng", dstLng);    Log.d(TAG, "-> sendNotification -> Received parameters:\nmessage:"            + msg + "\nsrcLat=" + srcLat + "\nsrcLng=" + srcLng            + "\ndstLat=" + dstLat + "\ndstLng=" + dstLng);    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,            intent, 0);    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(            this).setSmallicon(R.drawable.ic_launcher)            .setContentTitle("New Taxi Request Received")            .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))            .setContentText(msg);    mBuilder.setContentIntent(contentIntent);    mnotificationmanager.notify(NOTIFICATION_ID, mBuilder.build());}}

这是我的Activity,它会在点击通知时启动.

public class ShowUser extends Activity {private TextVIEw messageTextVIEw;private TextVIEw showUsernameTextVIEw;private TextVIEw srcLatTextVIEw;private TextVIEw srcLngTextVIEw;private TextVIEw dstLatTextVIEw;private TextVIEw dstLngTextVIEw;private SharedPreferences prefs;private String username;private static final String TAG = "ShowUser";@OverrIDeprotected voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentVIEw(R.layout.activity_show_user_details);    Intent intent = getIntent();    String message = intent.getStringExtra("message");    String srcLat = intent.getStringExtra("srcLat");    String srcLng = intent.getStringExtra("srcLng");    String dstLat = intent.getStringExtra("dstLat");    String dstLng = intent.getStringExtra("dstLng");    Log.d(TAG, "ReceivedExtras: message=" + message            + "srcLat=" + srcLat + "srcLng=" + srcLng            + "dstLat=" + dstLat + "dstLng=" + dstLng);    prefs = getSharedPreferences(DriverLoginActivity.USER_CREDENTIALS, Context.MODE_PRIVATE);    username = prefs.getString(DriverLoginActivity.USERname, null);    if(username!= null) {        messageTextVIEw = (TextVIEw) findVIEwByID(R.ID.messageTextVIEw);        showUsernameTextVIEw = (TextVIEw) findVIEwByID(R.ID.showUsernameTextVIEw);        srcLatTextVIEw = (TextVIEw) findVIEwByID(R.ID.sourceLatTextVIEw);        srcLngTextVIEw = (TextVIEw) findVIEwByID(R.ID.sourceLngTextVIEw);        dstLatTextVIEw = (TextVIEw) findVIEwByID(R.ID.destinationLatTextVIEw);        dstLngTextVIEw = (TextVIEw) findVIEwByID(R.ID.destinationLngTextVIEw);        showUsernameTextVIEw.setText("Welcome " + username);        messageTextVIEw.setText(message);        srcLatTextVIEw.setText("Source Latitude: " + srcLat);        srcLngTextVIEw.setText("Source Longitude: " + srcLng);        dstLatTextVIEw.setText("Destination Latitude: " + dstLat);        dstLngTextVIEw.setText("Destination Longitude: " + dstLng);    }}}

我认为它可能与PendingIntent有关,但我不确定这里发生了什么,因为我在AndroID中使用PendingIntents和通知相对较新.谁能告诉我发生了什么事?

解决方法:

在sendNotification中,您可以使用以下代码获取PendingIntent:

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

第一次执行此 *** 作时,AndroID将使用您提供的Intent作为此调用的第3个参数创建新的PendingIntent.以下时间,AndroID不会创建新的PendingIntent,但会返回引用您创建的第一个PendingIntent的标记.

如果您希望AndroID在下次执行此代码时替换原始PendingIntent上的“extras”,则需要在调用getActivity()时使用PendingIntent.FLAG_UPDATE_CURRENT作为第4个参数.这仍将返回一个令牌,该令牌引用您创建的第一个PendingIntent,但它也将替换原始PendingIntent中的所有“extras”,并将Intent中存在的“extras”作为第3个参数传递.

总结

以上是内存溢出为你收集整理的android – 为什么我的Activity总是从GCM通知中显示相同的数据集?全部内容,希望文章能够帮你解决android – 为什么我的Activity总是从GCM通知中显示相同的数据集?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1107470.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存