全网最大的精品网站,优秀的外贸网站案例,重庆新闻app,跨境电商的特点前言由于用uniapp官方的plus.push.createMessage()在Android平台下推送通知无法显示推送时间#xff0c;需要调用原生Android推送通知。在社区综合了各大神的代码#xff0c;具体如下#xff1a;推送推送事件#xff1a;/*** android原生通知发送* param content 通知内容*…前言由于用uniapp官方的plus.push.createMessage()在Android平台下推送通知无法显示推送时间需要调用原生Android推送通知。在社区综合了各大神的代码具体如下推送推送事件/*** android原生通知发送* param content 通知内容* param data json对象存储通知的隐藏数据用于点击通知时接收使用*/android_notify(content , data {}) {// #ifdef APP-PLUSvar title 通知标题;console.log(准备通知);console.log(plus.os.name);//Android平台下才使用此推送if (plus.os.name ! Android) {return false;}//随机生成通知IDvar 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,data:data};intent.putExtra(receive, JSON.stringify(payload));//PendingIntent.getActivity的第二个参数需要设置为随机数否则多个通知时会导致前面的通知被后面的通知替换Extra的数据var pendingIntent PendingIntent.getActivity(main, 0, 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.flagsNotification.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}调用方法android_notify(test, {params1: params1,params2: params2,params3: params3,params4: params4});接收点击通知接收通知数据/*** android原生通知接收*/android_receive() {// #ifdef APP-PLUSif (plus.os.name ! Android) {return false;}//android原生通知栏接收器(程序退出后无效)var main plus.android.runtimeMainActivity();var intent main.getIntent();var message intent intent.getExtra ! undefined ? intent.getExtra(receive) : null;console.log(message);message message ? JSON.parse(message) : ;if (message) {//删除当前通知// var Context plus.android.importClass(android.content.Context);// var nm main.getSystemService(Context.NOTIFICATION_SERVICE);// console.log(message.notify_id);//nm.cancel(s message.notify_id, message.notify_id);//安卓版本大于等于8的//nm.cancel(message.notify_id);//安卓版本小于8的// nm.cancelAll();//把消息数据置空以免再次打开APP时重复执行此处intent.putExtra(receive, );var params message.data;if (params1 xxx) {//TODO do something.} else if (params1 yyy) {//TODO do something.}}// #endif}调用方法// App.VueonShow: function() {...// #ifdef APP-PLUSandroid_receive();// #endif...},注意事项已知问题第一次通知会有通知延迟现象大概2-3秒后才会收到通知之后就不会了(官方的推送没有此问题)程序被关闭后点击通知消息无法再接收到通知的数据(暂时没有好的解决方案如果大家有好的方法欢迎回复分享)希望能帮助大家。2020年4月3日补充多行显示通知消息//创建大文本样式var bigTextStyle plus.android.importClass(android.support.v4.app.NotificationCompat$BigTextStyle);var style new bigTextStyle();style.setBigContentTitle(title);// style.setSummaryText(备注一行);style.bigText(content);//这里可以设置超长的文本mNotification.setStyle(style); //设置大文本样式以上代码放到var mNb mNotification.build();之前即可