• 0
  • 0

Uniapp 原生Android系统推送通知及点击通知监听数据

2020-03-26 1205 0 admin 所属分类:Hbuilder

由于用uniapp官方的plus.push.createMessage()在Android平台下推送通知无法显示推送时间,需要调用原生Android推送通知。具体如下: 

1.推送事件:

/**
 * android原生通知发送
 * @param title 通知标题
 * @param content 通知内容
 * @param data json对象,存储通知的隐藏数据,用于点击通知时接收使用
 */
android_notify(title, content = '', data = {}) {
    // #ifdef APP-PLUS
    console.log('准备通知');
    console.log(plus.os.name);
    //Android平台下才使用此推送
    if (plus.os.name != 'Android') {
        return false;
    }
    //随机生成通知ID
    var NotifyID = Math.floor(Math.random() * 10000) + 1;
    var main = plus.android.runtimeMainActivity();
    var Context = plus.android.importClass("android.content.Context");
    var NotificationManager = plus.android.importClass("android.app.NotificationManager");
    var nm = main.getSystemService(Context.NOTIFICATION_SERVICE);
    var Notification = plus.android.importClass("android.app.Notification");
    var Intent = plus.android.importClass("android.content.Intent");
    var PendingIntent = plus.android.importClass("android.app.PendingIntent");
    var intent = new Intent(main, main.getClass());
    //传递参数
    var payload = {
        'msg': content,
        'notify_id': NotifyID,
        'payload': data
    };
    intent.putExtra("receive", JSON.stringify(payload));
    // 第二个参数不能为0 这里置随机数 NotifyID 否则消息会覆盖 点击其他消息会没反应
    var pendingIntent = PendingIntent.getActivity(main, NotifyID, intent, PendingIntent.FLAG_CANCEL_CURRENT);
    var SystemVersion = plus.os.version;
    var firstVersionNumber = Number(SystemVersion.split('.')[0]);
    var r = plus.android.importClass("android.R");
    var mNotification;
    //判断当前系统版本在8.0及以上
    if (firstVersionNumber >= 8) {
        var NotificationChannel = plus.android.importClass('android.app.NotificationChannel');
        var channel = new NotificationChannel("s" + NotifyID, "1", NotificationManager.IMPORTANCE_HIGH);
        nm.createNotificationChannel(channel);
        Notification = plus.android.importClass("android.support.v4.app.NotificationCompat");
        mNotification = new Notification.Builder(main, "s" + NotifyID);
    } else {
        Notification = plus.android.importClass("android.app.Notification");
        mNotification = new Notification.Builder(main);
    }
    mNotification.setContentTitle(title) //设置标题
    mNotification.setContentText(content); //设置内容
    //mNotification.setSubText('');        //子内容暂时去掉
    mNotification.setAutoCancel(true); //设置点击消失
    mNotification.setShowWhen(true); //显示通知时间,貌似不加这句也能显示
    mNotification.setTicker("PadInfo"); //弹出通知
    mNotification.setSmallIcon(17301620); //设置当前app图标
    //mNotification.setDefaults(Notification.DEFAULT_VIBRATE);  //声音、闪灯、震动效果,可叠加,此语句无效
    mNotification.setPriority(Notification.PRIORITY_DEFAULT); //通知优先级  
    mNotification.flags = Notification.FLAG_ONLY_ALERT_ONCE; //发起通知时震动  
    mNotification.setContentIntent(pendingIntent);
    var mNb = mNotification.build();
    //判断当前系统版本在8.0及以上  
    if (firstVersionNumber >= 8) {
        nm.notify("s" + NotifyID, NotifyID, mNb);
    } else {
        nm.notify(NotifyID, mNb);
    }
    //void plus.device.beep(2);//bee bee叫
    plus.device.vibrate(300); //震动
    console.log('通知结束');
    return true;
    // #endif
}

1.1推送方法封装: 

/**
 * 推送封装 兼容 IOS android
 * @param title 通知内容
 * @param data json对象,存储通知的隐藏数据,用于点击通知时接收使用
 */
push_notify(title, data) {
    // #ifdef APP-PLUS
    if (plus.os.name == "Android") {
        plus.runtime.getProperty(plus.runtime.appid, (wgtinfo) = > {
            console.log(wgtinfo);
            this.android_notify(wgtinfo.name, title, data);
        });
    } else {
        plus.push.createMessage(title, JSON.stringify(data), {
            cover: false
        });
    }
    // #endif
}

1.2 回调函数 main.js 全局方法注册: 

handlePush(message) {
    console.log(message.payload)
    message.payload = JSON.parse(message.payload)
    console.log(message.payload)
    if (message != undefined && message.payload != undefined && message.payload.type == 'download') {
        // TODO  do xxx
    } else if (message != undefined && message.payload != undefined && message.payload.type == 'order_new') {
        // TODO  do xxx
    }
}

1.3调用方法: 

api.push_notify('test', {
    kind: 'params.key',
    run_id: 'params.value.run_id',
    push_time: 'params.push_time',
    pushed_at: 'params.value.pushed_at'
});

2.点击通知接收通知数据: 

/**
 * android原生通知接收
 */
android_receive(callBack) {
    // #ifdef APP-PLUS
    if (plus.os.name != 'Android') {
        return false;
    }
    //android原生通知栏接收器(程序退出后无效)
    var main = plus.android.runtimeMainActivity();
    var intent = main.getIntent();
    var message = (intent && intent.getExtra != undefined && intent.getExtra != '') ? intent.getExtra("receive") : undefined;
    if (message != undefined) {
        //避免退入主界面重开依旧弹出
        intent.putExtra("receive", '');
    }
    console.log(message);
    message = message ? JSON.parse(message) : {};
    //兼容plus对象 payload 是json字符串格式的
    if (message.payload != undefined) {
        message.payload = JSON.stringify(message.payload);
    }
    console.log(message);
    if (callBack != undefined) {
        callBack(message);
    }
    // #endif
}

2.1监听push android 点击: 

// App.Vue
onShow: function() {
    ...
    // #ifdef APP-PLUS
    //传入通知处理方法作为回调 兼容IOS监听
    api.android_receive(this.handlePush);
    // #endif
    ...
},

2.2监听push ios 点击: 

// 外推消息被单击事件
plus.push.addEventListener('click', (message) = > {
    this.handlePush(message);
});

注意事项: 

  1. 已知问题:第一次通知会有通知延迟现象,大概2-3秒后才会收到通知,之后就不会了(官方的推送没有此问题);

  2. 程序被关闭后点击通知消息无法再接收到通知的数据;

  3. 当进程被杀掉之后 创建的消息都会消失,只能后期结合个推推送

 

 

 

 

 

 

 

 

返回顶部