草庐IT

android - 将 intent extras 从谷歌云消息接收器传递到 Activity 是空的

coder 2023-12-28 原文

我有一个监听云消息的广播接收器,我可以很好地显示通知,但是当用户单击通知时,我想将他们路由到带有 intent extras 的正确 Activity 。额外的总是空的。

我可以验证传递给挂起 Intent 的 Intent 在调试时确实有额外的东西。

我尝试了解 Activity onCreate() 方法中的 Intent

***编辑添加工作代码,有几点需要注意,如果任务已经开始,你不能指望调用 oncreate() 方法,所以看起来最好的地方是获得额外的东西onresume 方法。我需要在 Intent 和未决 Intent 上设置标志

@EReceiver
public class MyBroadcastReceiver extends BroadcastReceiver {

private static final String TAG = "googlecloudmessage";
private static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
private NotificationCompat.Builder builder;
private Context ctx;

@Pref
public MyPrefs_ myPrefs;

@Override
public void onReceive(Context context, Intent intent) {

    Log.d(TAG, "received gcm message");

    GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
    this.ctx = context;

    String messageType = gcm.getMessageType(intent);

    if (messageType != null) {

        if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {

        }
        else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {

        }
        else if (messageType.equalsIgnoreCase("gcm")) {
            myPrefs.notification().put(true);
            handleNotification(intent);
        }
    }
    setResultCode(Activity.RESULT_OK);
}

private void handleNotification(Intent intent) {

    Bundle bundle = intent.getExtras();

    String typeString = bundle.getString("type");
    String icon = bundle.getString("icon");

    String title = null;
    String body = null;
    Class<?> intentClass = null;
    Integer intentExtraId = null;

    if (typeString == null)
        return;

    int type = Integer.parseInt(typeString);

    switch (type) {

    case 1:

        intentClass = FriendsActivity_.class;

        title = bundle.getString("username");

        break;

    case 2:

        intentClass = UserProfileActivity_.class;

        title = bundle.getString("username");

        intentExtraId = Integer.parseInt(bundle.getString("user_id"));

        break;




    }


    mNotificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);


Intent i = new Intent();
    i.setClass(ctx, intentClass);
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP     | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    i.putExtra("gcm", true);
    if (intentExtraId != null) {

        i.putExtra("id", intentExtraId);
    }

    int requestID = (int) System.currentTimeMillis();

    PendingIntent pendingIntent = PendingIntent.getActivity(ctx, requestID, i,     PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(ctx);

    mBuilder.setSmallIcon(R.drawable.ic_stat_u);

    Bitmap bmp = null;

    try {
        bmp = Ion.with(ctx, ctx.getResources().getString(R.string.image_container) + icon).asBitmap().get();
    }
    catch (InterruptedException e) {

        e.printStackTrace();
    }
    catch (ExecutionException e) {

        e.printStackTrace();
    }

    if (icon != null) {
        mBuilder.setLargeIcon(bmp);
    }

    mBuilder.setContentTitle(title).setStyle(new NotificationCompat.BigTextStyle().bigText(body))
            .setContentText(body);

    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

}

Activity

 @Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    setIntent(intent);
}

@Override
protected void onResume() {

    super.onResume();

    Integer newId = getIntent().getExtras().getInt("id");

    id = newId;


}

最佳答案

这是您用来获取 PendingIntent 的调用:

PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0, i, 0);

如果系统中已经有一个匹配的PendingIntent,这个调用将只返回那个。它可能包含也可能不包含您想要的额外内容。为确保您的附加功能得到使用,您必须确保更新任何当前的 PendingIntent,如下所示:

PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);

关于android - 将 intent extras 从谷歌云消息接收器传递到 Activity 是空的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20562898/

有关android - 将 intent extras 从谷歌云消息接收器传递到 Activity 是空的的更多相关文章

  1. ruby-on-rails - RSpec:避免使用允许接收的任何实例 - 2

    我正在处理旧代码的一部分。beforedoallow_any_instance_of(SportRateManager).toreceive(:create).and_return(true)endRubocop错误如下:Avoidstubbingusing'allow_any_instance_of'我读到了RuboCop::RSpec:AnyInstance我试着像下面那样改变它。由此beforedoallow_any_instance_of(SportRateManager).toreceive(:create).and_return(true)end对此:let(:sport_

  2. ruby - rails 3 redirect_to 将参数传递给命名路由 - 2

    我没有找到太多关于如何执行此操作的信息,尽管有很多关于如何使用像这样的redirect_to将参数传递给重定向的建议:action=>'something',:controller=>'something'在我的应用程序中,我在路由文件中有以下内容match'profile'=>'User#show'我的表演Action是这样的defshow@user=User.find(params[:user])@title=@user.first_nameend重定向发生在同一个用户Controller中,就像这样defregister@title="Registration"@user=Use

  3. ruby-on-rails - 如何生成传递一些自定义参数的 `link_to` URL? - 2

    我正在使用RubyonRails3.0.9,我想生成一个传递一些自定义参数的link_toURL。也就是说,有一个articles_path(www.my_web_site_name.com/articles)我想生成如下内容:link_to'Samplelinktitle',...#HereIshouldimplementthecode#=>'http://www.my_web_site_name.com/articles?param1=value1¶m2=value2&...我如何编写link_to语句“alàRubyonRailsWay”以实现该目的?如果我想通过传递一些

  4. ruby-on-rails - 如何使用 Rack 接收 JSON 对象 - 2

    我有一个非常简单的RubyRack服务器,例如:app=Proc.newdo|env|req=Rack::Request.new(env).paramspreq.inspect[200,{'Content-Type'=>'text/plain'},['Somebody']]endRack::Handler::Thin.run(app,:Port=>4001,:threaded=>true)每当我使用JSON对象向服务器发送POSTHTTP请求时:{"session":{"accountId":String,"callId":String,"from":Object,"headers":

  5. ruby - 在 Ruby 中按名称传递函数 - 2

    如何在Ruby中按名称传递函数?(我使用Ruby才几个小时,所以我还在想办法。)nums=[1,2,3,4]#Thisworks,butismoreverbosethanI'dlikenums.eachdo|i|putsiend#InJS,Icouldjustdosomethinglike:#nums.forEach(console.log)#InF#,itwouldbesomethinglike:#List.iternums(printf"%A")#InRuby,IwishIcoulddosomethinglike:nums.eachputs在Ruby中能不能做到类似的简洁?我可以只

  6. SPI接收数据异常问题总结 - 2

    SPI接收数据左移一位问题目录SPI接收数据左移一位问题一、问题描述二、问题分析三、探究原理四、经验总结最近在工作在学习调试SPI的过程中遇到一个问题——接收数据整体向左移了一位(1bit)。SPI数据收发是数据交换,因此接收数据时从第二个字节开始才是有效数据,也就是数据整体向右移一个字节(1byte)。请教前辈之后也没有得到解决,通过在网上查阅前人经验终于解决问题,所以写一个避坑经验总结。实际背景:MCU与一款芯片使用spi通信,MCU作为主机,芯片作为从机。这款芯片采用的是它规定的六线SPI,多了两根线:RDY和INT,这样从机就可以主动请求主机给主机发送数据了。一、问题描述根据从机芯片手

  7. 安卓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,打开命令窗口,并将路

  8. ruby - 如何将 Puma::Configuration 传递给 Sinatra? - 2

    这是我的网络应用:classFront我是这样开始的(请不要建议使用Rack):Front.start!这是我的Puma配置对象,我不知道如何传递给它:require'puma/configuration'Puma::Configuration.new({log_requests:true,debug:true})说真的,怎么样? 最佳答案 配置与您运行的方式紧密相关puma服务器。运行的标准方式puma-pumaCLI命令。为了配置puma配置文件config/puma.rb或config/puma/.rb应该提供(参见examp

  9. jquery - 如何将 AJAX 变量从 jQuery 传递到他们的 Controller ? - 2

    我有一个电子邮件表格。但是我正在制作一个测试电子邮件表单,用户可以在其中添加一个唯一的电子邮件,并让电子邮件测试将其发送到该特定电子邮件。为了简单起见,我决定让测试电子邮件通过ajax执行,并将整个内容粘贴到另一个电子邮件表单中。我不知道如何将变量从我的HAML发送到我的Controllernew.html.haml-form_tagadmin_email_blast_pathdoSubject%br=text_field_tag'subject',:class=>"mass_email_subject"%brBody%br=text_area_tag'message','',:nam

  10. ruby - 如何将 lambda 传递给 Hash.each? - 2

    如何将lambda传递给hash.each,以便我可以重复使用一些代码?>h={a:'b'}>h.eachdo|key,value|end=>{:a=>"b"}>test=lambdado|key,value|puts"#{key}=#{value}"end>test.call('a','b')a=b>h.each&testArgumentError:wrongnumberofarguments(1for2)from(irb):1:in`blockinirb_binding'from(irb):5:in`each'from(irb):5from/Users/jstillwell/.rv

随机推荐