Android 终极推送方案(结束应用进程依然可以接收通知)

开发中总会遇到产品经理需求是:结束掉应用依然想接收到通知,iOS轻而易举就可以实现,但Android一直都是杀掉应用后就无法接收到通知!

网上给的结果大概是:

1、集成各大厂商的推送sdk;(太繁琐)

2、集成某一大品牌的推送sdk,通过应用之间相互调用的方案;(测试无效)

3、等待Android统一推送联盟方案出来;(时间未知)

今天告诉你一个方案,通过阿里推送的辅助渠道推送可以实现即使杀掉应用也可以接收到通知:

官方辅助渠道集成文档地址

提前准备:

1、各大开放平台的应用申请(小米、华为、OPPO、VIVO、魅族);

注意:有些开放平台是分开 push 功能的,需要在 push 功能区 开通/启用 推送功能

2、集成阿里推送sdk文档

3、在阿里云移动推送配置渠道平台的信息,如下图:

4、辅助渠道推送也需要后台服务器进行配合,具体参考移动推送辅助通道配置 的7.3场景解析中的(场景1:普通推送打开App + 辅助弹窗 或 场景2:普通推送打开Activity + 辅助弹窗)

集成流程很简单,就依照文档集成完全没问题:

下面我记录我自己的集成,不做说明(我只集成了小米、华为、oppo、vivo):

一、项目的gradle配置:

allprojects {
    repositories {
        //阿里推送需要
        maven {
            url 'http://maven.aliyun.com/nexus/content/repositories/releases'
        }
    }
}

app的gradle配置:

dependencies {
     //阿里推送
    api('com.aliyun.ams:alicloud-android-push:3.1.6') {
        transitive true
    }
    //阿里移动推送辅助通道
    api 'com.aliyun.ams:alicloud-android-third-push:3.0.9@aar'
    //华为推送SDK依赖
    api 'com.aliyun.ams:huawei-push:2.6.3.305'
    api 'com.aliyun.ams:huawei-push-base:2.6.3.305'
}

二、app模块的AndroidManifest.xml中的配置:

         <!-- 阿里的移动推送 -->
        <meta-data
            android:name="com.alibaba.app.appkey"
            android:value="xxxxxx" /> <!-- 请填写你自己的- appKey -->
        <meta-data
            android:name="com.alibaba.app.appsecret"
            android:value="xxxxxx" /> <!-- 请填写你自己的appSecret -->
        <!--华为推送-->
        <meta-data
            android:name="com.huawei.hms.client.appid"
            android:value="appid=xxxxxx" />
        <!--VIVO推送-->
        <meta-data
            android:name="com.vivo.push.api_key"
            android:value="xxxxxx" />
        <meta-data
            android:name="com.vivo.push.app_id"
            android:value="xxxxxx" />

三、项目的application中的初始化:

     /**
     * 初始化阿里云推送通道
     */
    private void initCloudChannel() {
        PushServiceFactory.init(this);
        CloudPushService pushService = PushServiceFactory.getCloudPushService();
        pushService.setDebug(true);
        pushService.register(this, new CommonCallback() {
            @Override
            public void onSuccess(String response) {
                LogUtil.d("init cloudchannel success");
            }

            @Override
            public void onFailed(String errorCode, String errorMessage) {
                LogUtil.d("init cloudchannel failed -- errorcode:" + errorCode + " -- errorMessage:" + errorMessage);
            }
        });
    }
     /**
     * 初始化辅助推送渠道
     */
    private void initThirdPushChannel() {
        // 注册方法会自动判断是否支持小米系统推送,如不支持会跳过注册。
        MiPushRegister.register(this, AppConstant.XIAOMI_APP_ID, AppConstant.XIAOMI_APP_Key);
        // 注册方法会自动判断是否支持华为系统推送,如不支持会跳过注册。
        HuaWeiRegister.register(this);
        // OPPO通道注册
        OppoRegister.register(this, AppConstant.OPPO_APP_KEY, AppConstant.OPPO_APP_SECRET); // appKey/appSecret在OPPO开发者平台获取
//        // 魅族通道注册
//        MeizuRegister.register(applicationContext, "appId", "appkey"); // appId/appkey在魅族开发者平台获取
        // VIVO通道注册
        VivoRegister.register(this);
    }

四、代码接收推送消息:

1、正常推送接收代码AliMessageReceiver:


public class AliMessageReceiver extends MessageReceiver {
    private String channelId;
    private NotificationManager notificationManager;

    @Override
    public void onNotification(Context context, String title, String summary, Map<String, String> extraMap) {
        //处理推送通知
        LogUtil.e("Receive notification, title: " + title + ", summary: " + summary + ", extraMap: " + extraMap);
    }

    @Override
    public void onMessage(Context context, CPushMessage cPushMessage) {
        //处理推送消息
        LogUtil.e("onMessage, messageId: " + cPushMessage.getMessageId() + ", title: " + cPushMessage.getTitle() + ", content:" + cPushMessage.getContent());
    }

    @Override
    public void onNotificationOpened(Context context, String title, String summary, String extraMap) {
        //点击通知后的操作
        LogUtil.e("onNotificationOpened, title: " + title + ", summary: " + summary + ", extraMap:" + extraMap);
    }

    @Override
    protected void onNotificationClickedWithNoAction(Context context, String title, String summary, String extraMap) {
        LogUtil.e("onNotificationClickedWithNoAction, title: " + title + ", summary: " + summary + ", extraMap:" + extraMap);
    }

    @Override
    protected void onNotificationReceivedInApp(Context context, String title, String summary, Map<String, String> extraMap, int openType, String openActivity, String openUrl) {
        LogUtil.e("onNotificationReceivedInApp, title: " + title + ", summary: " + summary + ", extraMap:" + extraMap + ", openType:" + openType + ", openActivity:" + openActivity + ", openUrl:" + openUrl);
    }

    @Override
    protected void onNotificationRemoved(Context context, String messageId) {
        //处理移除通知栏
        LogUtil.e("onNotificationRemoved");
    }
}

 2、辅助渠道接收代码PopupPushActivity(注意:此activity的绝对路径需要在服务端的代码中配置):

public class PopupPushActivity extends AndroidPopupActivity {
    static final String TAG = "PopupPushActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        overridePendingTransition(R.anim.anim_no, R.anim.anim_no);
    }

   
    @Override
    protected void onSysNoticeOpened(String title, String summary, Map<String, String> extMap) {
//点击通知栏后执行的方法
//        Log.e("sdsd", "OnMiPushSysNoticeOpened, title: " + title + ", content: " + summary + ", extMap: " + extMap);
     
    }
}

到这里就集成完了,可以去测试了,至于文档中(6. 在日志中查看初始化情况 )我集成后有些设备并没有输出集成成功的标志(如红米手机),所以至于检测是否初始化成功,有最好,没有就直接去测试推送,不要过分纠结浪费时间!

  • 0
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Android通知栏是用来显示消息的重要组件之一。要在Android应用中实现通知消息,你需要进行以下步骤: 1. 创建通知渠道:从Android 8.0(API级别26)开始,你需要创建通知渠道来管理和组织通知。使用NotificationChannel类来创建通知渠道,并设置其名称、描述和重要性级别等参数。 2. 构建通知内容:使用NotificationCompat.Builder类来构建通知的内容,包括标题、文本、图标、大图等。 3. 设置点击行为:可以为通知设置点击行为,比如打开应用的某个界面或执行特定的操作。使用PendingIntent类来定义点击通知时要执行的动作。 4. 发通知:通过NotificationManager类的notify()方法发通知。指定一个唯一的通知ID以及之前创建的NotificationCompat.Builder对象。 下面是一个示例代码,演示了如何发一个简单的通知: ```java // 创建通知渠道 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel("channel_id", "Channel Name", NotificationManager.IMPORTANCE_DEFAULT); channel.setDescription("Channel Description"); // 在NotificationManager中创建通知渠道 NotificationManager notificationManager = getSystemService(NotificationManager.class); notificationManager.createNotificationChannel(channel); } // 构建通知内容 NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "channel_id") .setSmallIcon(R.drawable.notification_icon) .setContentTitle("Notification Title") .setContentText("Notification Text") .setPriority(NotificationCompat.PRIORITY_DEFAULT) .setContentIntent(pendingIntent) // 设置点击行为 .setAutoCancel(true); // 点击后自动取消通知 // 发通知 NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); notificationManager.notify(notificationId, builder

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值