Android极光推送之前台弹出对话框

Android开发中,经常遇到推送信息,笔者最近也遇到开发中当应用在前台运行时,需要弹出对话框,后台运行需要在通知栏里显示:由于之前没有做过相关的开发,并且在百度上也没有找到好的例子,于是就自己动手。写了一个!


具体的极光的配置这里就不再说了,首先我们要判别 应用是不是在前台,这里笔者用了简单的做法

首先写个几类BaseActivity  集成 Activity


public static int anInt=0;

 @Override
	 protected void onResume() {
	        super.onResume();
         	anInt++;
	    }

	    @Override
	    protected void onPause() {
	        super.onPause();
			anInt--;
	    }
	@Override
	protected void onStart() {
		super.onStart();
		anInt++;
	}

	@Override
	protected void onStop() {
		super.onStop();
		anInt--;
	}

这里只要Activity在前台运行 anint 都不会等于0.来做前台的判断:


然后自定义一个广播 MyReciver 继承  PushReciver

当接收到广播时做前台判断 如果在前台清除 通知栏  具体代码如下:

JPushInterface.clearAllNotifications(context);
private static final String TAG = "MyReceiver";
    private NotificationManager nm;
    @Override
    public void onReceive(Context context, Intent intent) {

        Bundle bundle = intent.getExtras();
        LogUtil.d(TAG, "onReceive - " + intent.getAction() + ", extras: "+bundle.get(JPushInterface.EXTRA_ALERT));
        LogUtil.e("at22","----"+MyApplication.getInstance().getAnInt());
        if (BaseActivity.anInt!=0){
            JPushInterface.clearAllNotifications(context);
           if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
//                Log.e(TAG, "接受到推送下来的通知");
               receivingNotification(context, bundle);
               openNotification(context, bundle);
               String message = bundle.getString(JPushInterface.EXTRA_ALERT);
               Activity currentActivity = TestActivityManager.getInstance().getCurrentActivity();
               showPopwindow(currentActivity, message);
               // }
           }
        }else {
            if (null == nm) {
                nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            }
            if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
                LogUtil.e(TAG, "JPush用户注册成功");

            } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
                LogUtil.e(TAG, "接受到推送下来的自定义消息");

            } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
                LogUtil.e(TAG, "接受到推送下来的通知");
                showNotication(context);
               // receivingNotification(context,bundle);

            } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
                LogUtil.e(TAG, "用户点击打开了通知");
                openNotification(context,bundle);
                String message = bundle.getString(JPushInterface.EXTRA_ALERT);

                if (message.contains("买家提醒您发货")){
                    Intent goods_Intent = new Intent(context, GoodsFancyActivity.class);
                    goods_Intent.putExtra("type", "sell");
                    //i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    goods_Intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP );
                    context.startActivity(goods_Intent);
                }
                if (message.contains("别忘了商品")){
                    Intent goods_Intent = new Intent(context, IntegramallActivity.class);
                    //i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    goods_Intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP );
                    context.startActivity(goods_Intent);
                }
                if (message.contains("获得了本期幸运奖品")){
                    Intent goods_Intent = new Intent(context, JiangPinRecoder.class);
                    //i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    goods_Intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP );
                    context.startActivity(goods_Intent);
                }
                if (message.contains("您的帐号已在另一台设备登陆!")){
                    MySPUtils.clear(context);
                    Intent goods_Intent = new Intent(context, LoginActivity.class);
                    //i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    goods_Intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP );
                    context.startActivity(goods_Intent);
                }

                if (message.contains("审核")){
                    Intent exIntent = new Intent(context, ExamMoneyRecoderdActivity.class);
                    //i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    exIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP );
                    context.startActivity(exIntent);
                }

            } else {
                LogUtil.e(TAG, "Unhandled intent - " + intent.getAction());
            }
        }
    }

这样就可以实现 在前台 显示对话框了,小编实在没有找到好的办法实现 ,如果有好的方法请联系小编啊!



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值