第三方推送过来的消息,用户点击如何处理

第三方推送过来的消息,用户点击如何处理

第三方推送过来的消息,用户点击通知栏一点点小想法,如果有不对的地方,欢迎指正:

  • 需求: 用户点击通知栏跳转到指定页面。

  • 场景一

  • 这个时候又分两种情况,一种是app在前台,即用户还在操作app,处于app某个界面,另外一种是用户点击home键,其实说到这两种可以用一段代码来解决,因为这是时候app还活着,没有被干掉,参考代码示例如下
//判断app进程是否存活
if(MainTabFragmentActivity.isRunInMemory){
    //如果存活的话,FanForumActivity,但要考虑一种情况,就是app的进程虽然仍然在
    //但Task栈已经空了,比如用户点击Back键退出应用,但进程还没有被系统回收,如果直接启动
    Log.i("NotificationReceiver", "the app process is alive");
    Intent mainIntent = new Intent(context, MainTabFragmentActivity.class);
    mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    Intent   forunIntent=new Intent(context,FanForumActivity.class);
    forunIntent.putExtra(Run.TAG_INDEX,2);
    forunIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    Intent[] intents = {mainIntent, forunIntent};
    context.startActivities(intents);
}
  • 上面有个判断如何判断app是否存活,我是通过一个布尔值记住主的Activity,是否存活,参考示例
public static   boolean  isRunInMemory=false;



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

   isRunInMemory=true;
}


@Override
protected void onDestroy() {
   super.onDestroy();
   isRunInMemory=false;
}

我之前也在网上找到一个方法来判断app是否存活,发现有问题,要么在不同版本上返回有问题,已经不适用,感兴趣的朋友可以查查,也有方法来代替检测app是否存活.有问题方法示例:

/**
 * 判断应用进程是否已经存在
 * @param context 一个context
 * @param packageName 要判断应用的包名
 * @return boolean
 */
public static boolean isAppAlive(Context context, String packageName){
    ActivityManager activityManager =
            (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> processInfos
            = activityManager.getRunningAppProcesses();
    for(int i = 0; i < processInfos.size(); i++){
        if(processInfos.get(i).processName.equals(packageName)){
            Log.i("NotificationLaunch",
                    String.format("the %s is running, isAppAlive return true", packageName));
            return true;
        }
    }
    Log.i("NotificationLaunch",
            String.format("the %s is not running, isAppAlive return false", packageName));
    return false;
}
  • 场景二
  • 前面说的是点击通知栏的普通情况,还有一种情况就是app已经死掉了,但是通知栏消息还没有消失,为什么通知栏没有消息?我发现现在系统当app被用户清掉后,如果这个app开启自启动,通知栏权限,在这种情况下,app之前接受到的消息还会出现在通知栏,这就尴尬了,所以我们还要做一个处理,app死掉后唤醒app,代码示例
**
 * 打开一个app
 *
 * @param packageName
 * @param data
 * @return
 */
public static boolean lanuchApp(Context context,String packageName, Bundle data,String type,int parameter) {
    try {
        Intent launchIntent = context.getPackageManager().
                getLaunchIntentForPackage(packageName);
        launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Bundle args = new Bundle();
        args.putString(Run.NOTIFIC_MODE, type);
        args.putInt(Run.TAG_INDEX,parameter);
        launchIntent.putExtra(Constants.EXTRA_BUNDLE, args);
        context.startActivity(launchIntent);
        return true;

    } catch (Exception e) {
    }
    return false;
}

场景二的做法就是,唤醒应用启动页,将自己所要带入的参数通过Bundle ,记住带入下一个activity,而下一个activity就是你的主界面,在主的activity中处理决定跳转指定页面逻辑:

启动页代码逻辑示例:

Intent  startIntent=    new Intent(getBaseContext(), ThirdBureauMainActivity.class);
if(getIntent().getBundleExtra(Constants.EXTRA_BUNDLE) != null){
   Bundle bundle=null;
   bundle = getIntent().getBundleExtra(Constants.EXTRA_BUNDLE);
   type = bundle.getString(Run.NOTIFIC_MODE);
   selectIndex = bundle.getInt(Run.TAG_INDEX,0);

   LogUtils.instance.i("handleMessage","type=>"+type+"selectIndex=>"+selectIndex);
   startIntent.putExtra(Run.NOTIFIC_MODE,type);
   startIntent.putExtra(Run.TAG_INDEX,selectIndex);
}

主界面一:

Intent  intent=   getIntent();
    //点击通知栏
    String notific_mode =  intent.getStringExtra(Run.NOTIFIC_MODE);
    int  selectIndex=intent.getIntExtra(Run.TAG_INDEX,0);
    if (Run.NOTIFIC_TYPE.equals(notific_mode)||Run.NOTIFIC_EMPTY_TYPE.equals(notific_mode))
    {
        startActivity(new Intent(ThirdBureauMainActivity.this, MainTabFragmentActivity.class).putExtra(Run.NOTIFIC_MODE,notific_mode).putExtra(Run.TAG_INDEX,selectIndex));
    }
}

主界面二:

String  notific_mode= getIntent().getStringExtra(Run.NOTIFIC_MODE);
int  selectIndex=getIntent().getIntExtra(Run.TAG_INDEX,0);

if (Run.NOTIFIC_TYPE.equals(notific_mode))
{
   Intent   intent=new Intent(this,FanForumActivity.class);
   intent.putExtra(Run.TAG_INDEX,selectIndex);
   startActivity(intent);
}else {//只是启动页面不操作

}

我们app是由两个app的功能,主界面一是一个选择界面,主界面二才是真的主界面。所以要传递参数3次。这样虽然复杂但是能实现功能= =,就这样吧。

感谢神吧:
非常感谢一些大神的分享经历:
参考:http://www.jianshu.com/p/224e2479da18

碎碎念:
1.推送必然会提到保活,目前安卓N版对权限这块处理优化过后更难,但是需求又要完成,这里提供一个参考的的GitHub地址,https://github.com/IDBAI/Daemon-simple
2.推送sdk目前用的极光

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值