草庐IT

android - GCM android 推送通知始终显示旧消息。收到的 Intent 不正确

coder 2023-12-08 原文

我有一个完美运行的 GCM 通知实现。但是,问题是一旦在 received 方法的 Intent 中接收到消息,显示的消息始终是旧消息。那就是“extras.getString("payload")”总是显示旧消息。我似乎无法弄清楚问题出在哪里。

发送 GCM 通知的类是:

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class C2DMMessageReceiver extends BroadcastReceiver {
            @Override
            public void onReceive(Context context, Intent intent) {
                            String action = intent.getAction();
                            Log.w("C2DM", "Message Receiver called");
                            if ("com.google.android.c2dm.intent.RECEIVE".equals(action)) {
                                            Log.w("C2DM", "Received message");
                                            String payload = intent.getStringExtra("payload");
                                            Log.d("C2DM", "dmControl: payload = " + payload);
                                            // TODO Send this to my application server to get the real data
                                            // Lets make something visible to show that we received the message
                                            createNotification(context, payload);
                            }
            }



            public void createNotification(Context context, String payload) {
                           NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
                            Notification notification = new Notification(R.drawable.icon,
                                                            "Message sent!", System.currentTimeMillis());
                            // Hide the notification after its selected
                            //notification.flags |= Notification.FLAG_AUTO_CANCEL;
                            notification.ledARGB = 0xff00ff00;
                            notification.ledOnMS = 300;
                            notification.ledOffMS = 1000;
                            notification.flags |= Notification.FLAG_SHOW_LIGHTS;


                            Intent intent = new Intent(context, MessageReceivedActivity.class);
                            intent.putExtra("payload", payload);
                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            intent.putExtra("NotifID", 1);
                            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,intent, 0);
                            notification.setLatestEventInfo(context, "Message","Message Recieved", pendingIntent);
                            notificationManager.notify(0, notification);
            }

接收通知消息的类是:

import android.app.Activity;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.widget.TextView;

public class MessageReceivedActivity extends Activity {
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                            setContentView(R.layout.activity_result);
                            NotificationManager notificationManager = (NotificationManager) this
                                                            .getSystemService(Context.NOTIFICATION_SERVICE);
                            //---cancel the notification---
                            int id=getIntent().getExtras().getInt("NotifID");
                            notificationManager.cancelAll();       
                            Bundle extras = getIntent().getExtras();
                            if (extras != null) {
                                            String message = extras.getString("payload");
                                            if (message.equals("call")) {
                                                            Intent intent = new Intent(Intent.ACTION_CALL);
                                                            intent.setData(Uri.parse("tel:9916261960"));
                                                            startActivity(intent);
                                            } else if (message.equals("camera")) {
                                                            Intent cameraIntent = new Intent(
                                                                                            MediaStore.ACTION_IMAGE_CAPTURE);
                                                            startActivity(cameraIntent);
                                            } else {
                                                            if (message != null && message.length() > 0) {
                                                                            TextView view = (TextView) findViewById(R.id.result);
                                                                            view.setText(message);
                                                            }
                                            }
                            }
                            super.onCreate(savedInstanceState);
            }

在这里,extras.getString("payload");始终保留第一次发送的通知消息。

最佳答案

创建待定 Intent 时,请使用 FLAG_UPDATE_CURRENT

PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

否则旧的 Intent 将在没有新的额外内容的情况下被重用

关于android - GCM android 推送通知始终显示旧消息。收到的 Intent 不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13411952/

有关android - GCM android 推送通知始终显示旧消息。收到的 Intent 不正确的更多相关文章

  1. ruby-on-rails - Rails 编辑表单不显示嵌套项 - 2

    我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib

  2. ruby-on-rails - Rails 常用字符串(用于通知和错误信息等) - 2

    大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje

  3. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  4. ruby-on-rails - 使用 Sublime Text 3 突出显示 HTML 背景语法中的 ERB? - 2

    所以我在关注Railscast,我注意到在html.erb文件中,ruby代码有一个微弱的背景高亮效果,以区别于其他代码HTML文档。我知道Ryan使用TextMate。我正在使用SublimeText3。我怎样才能达到同样的效果?谢谢! 最佳答案 为SublimeText安装ERB包。假设您安装了SublimeText包管理器*,只需点击cmd+shift+P即可获得命令菜单,然后键入installpackage并选择PackageControl:InstallPackage获取包管理器菜单。在该菜单中,键入ERB并在看到包时选择

  5. ruby-on-rails - link_to 不显示任何 rails - 2

    我试图在索引页中创建一个超链接,但它没有显示,也没有给出任何错误。这是我的index.html.erb代码。ListingarticlesTitleTextssss我检查了我的路线,我认为它们也没有问题。PrefixVerbURIPatternController#Actionwelcome_indexGET/welcome/index(.:format)welcome#indexarticlesGET/articles(.:format)articles#indexPOST/articles(.:format)articles#createnew_articleGET/article

  6. ruby-on-rails - 如何在 Rails View 上显示错误消息? - 2

    我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c

  7. ruby-on-rails - 复数 for fields_for has_many 关联未显示在 View 中 - 2

    目前,Itembelongs_toCompany和has_manyItemVariants。我正在尝试使用嵌套的fields_for通过Item表单添加ItemVariant字段,但是使用:item_variants不显示该表单。只有当我使用单数时才会显示。我检查了我的关联,它们似乎是正确的,这可能与嵌套在公司下的项目有关,还是我遗漏了其他东西?提前致谢。注意:下面的代码片段中省略了不相关的代码。编辑:不知道这是否相关,但我正在使用CanCan进行身份验证。routes.rbresources:companiesdoresources:itemsenditem.rbclassItemi

  8. ruby-on-rails - 如何在发布新的 Ruby 或 Rails 版本时收到通知? - 2

    有人知道在发布新版本的Ruby和Rails时收到电子邮件的方法吗?他们有邮件列表,RubyonRails有一个推特,但我不想听到那些随之而来的喧嚣,我只想知道什么时候发布新版本,尤其是那些有安全修复的版本。 最佳答案 从therailsblog获取提要.http://weblog.rubyonrails.org/feed/atom.xml 关于ruby-on-rails-如何在发布新的Ruby或Rails版本时收到通知?,我们在StackOverflow上找到一个类似的问题:

  9. ruby-on-rails - 在 Flash 警报 Rails 3 中显示错误消息 - 2

    如果我在模型中设置验证消息validates:name,:presence=>{:message=>'Thenamecantbeblank.'}我如何让该消息显示在闪光警报中,这是我迄今为止尝试过的方法defcreate@message=Message.new(params[:message])if@message.valid?ContactMailer.send_mail(@message).deliverredirect_to(root_path,:notice=>"Thanksforyourmessage,Iwillbeintouchsoon")elseflash[:error]

  10. 安卓apk修改(Android反编译apk) - 2

    最近因为项目需要,需要将Android手机系统自带的某个系统软件反编译并更改里面某个资源,并重新打包,签名生成新的自定义的apk,下面我来介绍一下我的实现过程。APK修改,分为以下几步:反编译解包,修改,重打包,修改签名等步骤。安卓apk修改准备工作1.系统配置好JavaJDK环境变量2.需要root权限的手机(针对系统自带apk,其他软件免root)3.Auto-Sign签名工具4.apktool工具安卓apk修改开始反编译本文拿Android系统里面的Settings.apk做demo,具体如何将apk获取出来在此就不过多介绍了,直接进入主题:按键win+R输入cmd,打开命令窗口,并将路

随机推荐