[Android]接收Push Notification及弹出Dialog

1、使用一个service接收gcm

public class MyGcmListenerService extends GcmListenerService {

    private static final String TAG = "MyGcmListenerService";


    /**
     * Called when message is received.
     *
     * @param from SenderID of the sender.
     * @param data Data bundle containing message data as key/value pairs.
     *             For Set of keys use data.keySet().
     */
    // [START receive_message]
    @Override
    public void onMessageReceived(String from, Bundle data) {
        String message = data.getString("message");
        Log.d(TAG, "From: " + from);
        Log.d(TAG, "Message: " + message);

        for (String key : data.keySet() ) {
            Log.d(TAG, "key : " + key);
        }

        String messageId = data.getString("google.message_id");
        String messageKey = data.getString("message");
        String collapse_key = data.getString("collapse_key");
        String score = data.getString("score");

        String payload = data.getString("data.payload");
        String extraMap = data.getString("data.extraMap");

        Log.d(TAG, "Message id : " + messageId);
        Log.d(TAG, "message_key : " + messageKey + ",collapse_key : " + collapse_key + ",payload : " + payload
                + ",extraMap : " + extraMap);

        if (message == null || message.equals("")) {
            Log.d(TAG, "Received message from GCM not success...");
        } else {

            Log.d(TAG, "success received from GCM : " + message);


        }

        if (from.startsWith("/topics/")) {
            // message received from some topic.
            Log.d(TAG, "message received from some topic.");
        } else {
            // normal downstream message.
            Log.d(TAG, "normal downstream message.");

        }

        // [START_EXCLUDE]
        /**
         * Production applications would usually process the message here.
         * Eg: - Syncing with server.
         *     - Store message in local database.
         *     - Update UI.
         */

        /**
         * In some cases it may be useful to show a notification indicating to the user
         * that a message was received.
         */
        sendNotification(message,score);

        // [END_EXCLUDE]

    }

。。。。。。

 

2.建立推送(两个功能部分:1.点击Notification跳转AlertDialog 2.在MainActivity中弹框(使用LocalBroadcast)

private void sendNotification(String title,String content) {
        Intent intent = new Intent(this, AlertActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("title",title);
        bundle.putString("content",content);
        intent.putExtra("alert_gcm", bundle);

        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//PendingIntent 是点击推送后打开的Activity,第三个参数Intent可以设定跳转
//这里是跳转到一个自定义的AlertActivity中,里面打开AlertDialog(可以把Activity底色设为透明)
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_stat_ic_notification)
                .setContentTitle(title)
                .setContentText(content)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());

        //MainActivity to received gcm notification
        Intent gcmComplete = new Intent(QuickstartPreferences.GCM_COMPLETE);

        Log.d(TAG, "title : " + title + " content : " + content);

        gcmComplete.putExtra("gcmBundle", bundle);
        LocalBroadcastManager.getInstance(this).sendBroadcast(gcmComplete);

    }
若点击Notification传值失败,一般调节一个flag的值:
flags有四个取值:
int FLAG_CANCEL_CURRENT:如果该PendingIntent已经存在,则在生成新的之前取消当前的。
int FLAG_NO_CREATE:如果该PendingIntent不存在,直接返回null而不是创建一个PendingIntent.
int FLAG_ONE_SHOT:该PendingIntent只能用一次,在send()方法执行后,自动取消。
int FLAG_UPDATE_CURRENT:如果该PendingIntent已经存在,则用新传入的Intent更新当前的数据。

我们需要把最后一个参数改为PendingIntent.FLAG_UPDATE_CURRENT,这样在启动的Activity里就可以用接收Intent传送数据的方法正常接收。

 

3.MainActivity建立接收器:

mGcmNotificationBroadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                Bundle bundle = intent.getBundleExtra("gcmBundle");
                String title = bundle.getString("title");
                String content = bundle.getString("content");
                Log.d(TAG, "title : " + title + " content : " + content);
                //Dialog to show content
                new AlertDialog.Builder(MainActivity.this,R.style.dialogTheme)
                        .setTitle(title)
                        .setMessage(content)
                        .setNegativeButton("OK", null)
                        .show();
            }
        };

 

注册接收器:

private void registerReceiver(){
 
        if (!isReceiverGcm){
            LocalBroadcastManager.getInstance(this).registerReceiver(mGcmNotificationBroadcastReceiver,
                    new IntentFilter(QuickstartPreferences.GCM_COMPLETE));
            isReceiverGcm = true;
        }
    }

 

@Override
    protected void onResume() {
        super.onResume();
        registerReceiver();

    }

    @Override
    protected void onPause() {
        LocalBroadcastManager.getInstance(this).unregisterReceiver(mGcmNotificationBroadcastReceiver);
        isReceiverGcm = false;
        super.onPause();
    }

 

4.AlertActivity:

Intent intent = getIntent();
        Bundle bundle = intent.getBundleExtra("alert_gcm");
        String title = bundle.getString("title");
        String content = bundle.getString("content");
        Log.d(TAG, "title : " + title + " content : " + content);
        //Dialog to show content
        new AlertDialog.Builder(this,R.style.dialogTheme)
                .setTitle(title)
                .setMessage(content)
                .setNegativeButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent i = new Intent(AlertActivity.this, MainActivity.class);
                        startActivity(i);
                    }
                })
                .show();

 

android全屏弹出框 android全屏弹出框,AlertDialog 立即下载

相关推荐

深入NAND Flash信号与接口:结合ONFI 5.0协议,聊聊DBI、ODT和差分信号到底在解决什么实际问题

本文深入探讨了NAND Flash信号与接口设计,结合ONFI 5.0协议,详细解析了DBI、ODT和差分信号等技术如何解决高速数据传输中的信号完整性问题。通过实际案例和数据,展示了这些技术在降低功耗、减少噪声和提升抗干扰能力方面的显著效果,为存储工程师提供了宝贵的实践参考。

weixin_33736649的博客 343

AlertActivity

对话框的实现

Alone 1377

用户画像标签维度_搭建电商平台用户画像标签体系

01 整体思路电商平台连接了卖家和买家两端,最终目的都是促进交易,从而追求更高的销售额。提高人货匹配度是促进交易的关键,而提高人货匹配度的关键就是“让买家找到想要的货物”。本文结合“人、货、场”理论模型,尝试建立电商平台的用户画像标签体系。02 三类标签用户在平台购物的整体流程可以概括为:进入平台、选择商品、下单付款。可以将该过程抽象为“人、货、场”。即用户通过什么途径进入什么商品的页面...

weixin_31668621的博客 1642

Anroid对话框——AlertActivity对话框 PopWindow弹出对话框(二)

Anroid对话框——AlertActivity对话框 PopWindow弹出对话框 <!--渐变 <gradient android:angle="45" android:startColor="@color/colorPrimary" android:endColor="#5555"/>-->

zhangyufeng0126的博客 4945

DTS中video-interfaces接口详解

视频数据管道通常由外部设备组成,例如通过I2C、SPI或UART总线控制的摄像机传感器,以及SoC内部IP块(包括视频DMA引擎和视频数据处理器)。 SoC内部块由DT节点描述,与其他SoC块放置方式类似。外部设备表示为它们各自总线控制器节点的子节点,例如I2C。 所有视频设备上的数据接口都由它们的子“port”节点描述。端口的配置取决于参与数据传输的其他设备,并由“endpoint”子节...

龙图腾的博客 9008

Geant4学习一:写一个简单程序

1、 基本介绍

ccwwNUAA的博客 7697

Android对话框

Android的各大对话框 Android几大对话框的使用:普通对话框,自定义对话框,圆形进度条对话框,线性进度条对话框,日历对话框,时间对话框 普通对话框 AlertDialog类中有一个静态内部类Builder,通过AlertDialog.Builder来调用,从而实现对话框的使用,在此之前,我们先创一个带有单击事件的按钮实现对话框: &lt;Button andr...

qq_38842722的博客 1394

Innovus ECO技巧:用setEcoMode+ecoAddRepeater实现批量buffer替换的5个关键步骤

本文详细介绍了在Cadence Innovus工具中,利用setEcoMode与ecoAddRepeater命令实现高效、批量Buffer/Inverter替换的工程化策略。通过启用批处理模式、构建可维护的替换规则、掌握高级定位技巧以及规避布线陷阱等5个关键步骤,帮助芯片设计工程师在ECO阶段大幅提升修改效率与质量,确保设计稳定签核。

d6e7f8的博客 1117

妙用AlertActivity的mAlertParams 和 mAlert

final AlertController.AlertParams p = mAlertParams; p.mCursor = mCursor; p.mOnClickListener = mRingtoneClickListener; p.mLabelColumn = MediaStore.Audio.Media.TITLE; ...

weixin_30335353的博客 403

【LVGL】从HTML到LVGL:嵌入式UI的设计迁移与落地实践

嵌入式UI迁移实践:从HTML到LVGL 本文介绍了将HTML/CSS设计的UI原型迁移到嵌入式系统(LVGL)的完整流程,重点涉及"HTML→Figma→LVGL"的三步转换方法。具体内容包括: 设计迁移流程:通过Figma作为中间桥梁,将HTML原型转化为标准设计资产,再适配到LVGL库; 工具与参数准备:详细列出了设计迁移所需的工具链和需要提取的核心设计参数; Figma还原步骤:分组件讲解如何在Figma中1:1还原HTML设计,包括标题栏、压力显示区、图表区等核心组件; LVG

ymlmail的博客 1971

从数据手册到代码:一步步解读SC7A20寄存器配置与加速度值换算原理

本文详细解析了SC7A20三轴加速度传感器的寄存器配置与加速度值换算原理。从数据手册出发,逐步拆解关键寄存器设置,并推导原始ADC值到物理加速度的完整换算过程,帮助开发者深入理解传感器底层工作原理,提升嵌入式开发效率。

weixin_29179311的博客 366

安卓中dialog从地上弹出来效果。

安卓中dialog的两种用法,一种是嵌套布局的的dialog,一种是直接在代码中间写布局的用法。 首先是加载布局文件xml的用法: private void showDialog() { final AlertDialog dialog = new AlertDialog.Builder(Activity.this) .create(); dialog.sh

zqr772791008的博客 622

Python实战日志_P4_图书馆Web API服务

目标:使用Flask将图书馆系统升级为RESTful API服务,掌握Web开发基础难度:⭐⭐⭐⭐☆预计学习时间:75分钟前置要求:已完成第3篇《个人图书馆管理系统》

qq_39436086的博客 83

Android Dialog底部弹出菜单:从自定义样式到触摸交互全解析

本文全面解析Android底部弹出菜单的实现,从自定义Dialog样式、布局设计到触摸交互处理。详细介绍了如何通过继承Dialog类、定义窗口动画和设置点击事件监听器(View.OnClickListener)来创建流畅的用户体验,包括触摸外部关闭和动画效果优化等实用技巧。

w8x9y0z1的博客 1047

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

Android实现从底部弹出Dialog(带动画效果)

实现效果如图 1.dialog_pick_avatar.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" an...

qq_36356379的博客 3155

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

android dialog菜单,android 页面底部弹出菜单(dialog)

列举两种方法:方法一:使用dialog(背景会变暗)private void showDialog(Context context, @LayoutRes int resource) {//设置要显示的viewView view = View.inflate(context,resource,null);//此处可按需求为各控件设置属性view.findViewById(R.id.dialog_v...

weixin_35867979的博客 448

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

转:Dialog 实现底部弹出菜单

之前就想做这个底部弹出dialog的笔记,发现这篇已经写的很好,就搬过来方便自己看吧。 原文链接:http://www.cnblogs.com/xyc20080413/p/3461955.html   代码部分: ..... findViewById(R.id.button).setOnClickListener(new View.OnClickListener() { ...

becherpan 159

android背景底部投影,android:底部背景阴影的dialog弹出

style 样式:truetrue@anim/push_bottom_in@anim/push_bottom_out@null关键在于Theme.Dialog中的各种属性:falsetrue@nulltruetruetrue@nulltrue@color/background_floating_material_light@null@style/RtlOverlay.DialogWindowTit...

weixin_35767900的博客 516

android 页面底部弹出菜单(dialog)

列举两种方法:方法一:使用dialog(背景会变暗)private void showDialog(Context context, @LayoutRes int resource) { //设置要显示的view View view = View.inflate(context,resource,null); //此处可按需求为各控件设置属性 view.findVi...

JohnLiu的博客 6990

轻松实现dialog弹窗

记录一下传统的实现弹窗方式,实现一个简单的弹窗效果;  要实现一个弹窗我们首先要创建一个类去继承dialog,主题theme需要在style文件中添加以下标签。 style name="Dialog" parent="android:style/Theme.Dialog"> item name="android:background">#00000000item> item

qq_32413047的博客 791

EditTextPreference 下自定义弹出Dialog并获取

1自定义Dialog布局并加入EditTextPrefernce 自定义Dialog的布局文件 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap

Kenny的专栏 1万+

android 推送带链接,Android 接收推送消息跳转到指定页面的方法

问题的提出本次接入的是个推,其他家的推送没有研究过,思路应该是类似的App在前台,这个时候需要弹出一个对话框,提醒用户有新的消息,是否要查看,查看的话跳转到指定页面App在后台,或是App进程已经被系统回收,收到推送后App进程会被个推拉起。这时候要展示通知,点击通知栏打开App并跳转到目标页面,关闭目标页面后需要返回到应用首页,而不是直接推出App实现思路App在前台时,弹出Dialog提醒用户...

weixin_39676633的博客 605

android 如何把activity变成dialog

//设置在activity的底部,而且全屏 setContentView(layoutResId); getWindow().setGravity(Gravity.BOTTOM); getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); //如何配置成dialog,在配置文件中写上

1879

安卓系统webview更换_安卓日常开发问题总结:这些问题火候不够,随时变成删库跑路...

日常开发中碰到了各种开发问题,捡选了一部分分享出来。有些问题非常简单,属于编码规范类,有些属于特定情况下碰到的问题,不是很常见。不太准确的地方,欢迎共同探讨下~1.requestFeature() must be called before adding contentrequestWindowFeature(Window.FEATURE_NO_TITLE);// 隐藏标题需要在onCreate之...

weixin_39723102的博客 1191
上一篇: Resolved versions for app (24.2.1) and test app (24.0.0) differ.
下一篇: [IOS]Pull To Refresh 下拉刷新
jameskaron
博客等级 码龄20年 31粉丝 663原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值