Android通知栏,点击唤醒APP,跳转到指定Activity,终极方案

这个功能基本上每个app都会需要,希望能帮到你。

1、接收消息:接收推送的消息,一般有两种:自己的推送服务和第三方推送服务。不管是哪一种,都会按照app的需求接收到不同类型的消息,然后在需要弹notification的地方通知。这是句废话

2、弹出notification:

此时,需要指定一个PendingIntent,如果用户在app内部或是点击手机home键退到后台,此时ActivityManager栈中是有Activity的,跳到指定的activity,再点击返回,仍然在app内部;如果用户是双击退出,finish掉了MainActivity,即此时的ActivityManager是空的,跳转到指定Activity,再点击返回,就回到了手机桌面,不在app内部了。所以,此时需要放app中的MainActivity在PendingIntent内。

<activity
    android:name=".ctivity.MainTabActivity"
    android:screenOrientation="portrait"
    android:theme="@style/AppThemeStart">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

然后,弹出notification:此时可以把存有跳转信息的msg信息传给MainTabActivity

Intent intent = new Intent(context, MainTabActivity.class);
intent.putExtra("PushMsg", msg);
PendingIntent pi = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
notification(msg.getMsgTitle(defTitle), msg.getMsgText(), pi);

注意:在使用PendingIntent.getActivity的时候,最后一个参数flag,千万别设置0,因为如果设置为0的话,在MainTabActivity的onNewIntent和onCreate中拿不到“PushMsg”信息。

最后,创建Notification,此时需要注意Android SDK26,必须创建NotificationChannel。

private void notification(String title, String content, PendingIntent intent) {
    NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel = NotificationUtils.getRPushNotificationChannel();
        manager.createNotificationChannel(channel);
    }
    long when = System.currentTimeMillis();
    Notification notification = new NotificationCompat.Builder(context, NotificationUtils.CHANNEL_R_PUSH_ID)
            .setContentTitle(title)
            .setContentText(content)
            .setWhen(when)
            .setSmallIcon(R.mipmap.ic_launcher_round)
            .setContentIntent(intent)
            .setAutoCancel(true)
            .setPriority(NotificationCompat.PRIORITY_MAX)
            .build();
    manager.notify((int) when, notification);
}

public static NotificationChannel getPushNotificationChannel() {
    // Android8.0及以上NotificationChannel机制
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel mChannel = new NotificationChannel(CHANNEL_R_PUSH_ID, CHANNEL_R_PUSH_NAME, importance);
        // 配置通知渠道的属性
        mChannel.setDescription(CHANNEL_R_PUSH_DESC);
        // 设置通知出现时的闪灯(如果 android 设备支持的话)
        mChannel.enableLights(true);
        mChannel.setLightColor(Color.RED);
        // 设置通知出现时的震动(如果 android 设备支持的话)
        mChannel.enableVibration(true);
        mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
        return mChannel;
    } else {
        return null;
    }
}

3、在MainTabActivity中处理跳转到指定Activity的事情。

如果MainActivity在ActivityManager中,点击通知会进入onNewIntent方法:此时直接在intent参数中获取传递的数据。

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    Logger.t("MainTabActivity").i("onNewIntent");
    if (intent != null && intent.getExtras() != null) {
        PushEntity pushEntity = (PushEntity) intent.getExtras().getSerializable("PushMsg");
        startActivityForNotification(pushEntity);
    }
}

如果ActivityManager是空的,点击通知会进入onCreate方法:此时需要用getIntent()方法获取传递的数据。

@Override
public void onCreate() {
    Logger.t("MainTabActivity").i("getIntentData");
    Intent intent = getIntent();
    if (intent != null && intent.getExtras() != null) {
        PushEntity pushEntity = (PushEntity) intent.getExtras().getSerializable("PushMsg");
        //这个方法是做跳转到指定Activity用的,就不贴代码了
        //一般是根据消息类型写死跳转
        //或使用传递的数据,反射得到指定的class,再跳转
        startActivityForNotification(pushEntity);
    }
}

 

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值