h5 消息推送 jpush-phonegap-plugin 插件的使用

来源:互联网 发布:网络广告费价格 编辑:程序博客网 时间:2024/05/16 10:08

1.安装jpush-phonegap-plugin插件

安装命令 记得把your_jpush_appkey更换成自己的appkey,获取appkey的方法这里就不说了(官网地址:https://www.jiguang.cn/ )
  • 通过 Cordova Plugins 安装,要求 Cordova CLI 5.0+:

    cordova plugin add jpush-phonegap-plugin --variable API_KEY=your_jpush_appkey
  • 或直接通过 url 安装:

    cordova plugin add https://github.com/jpush/jpush-phonegap-plugin.git --variable API_KEY=your_jpush_appkey  
  • 或下载到本地安装:

    cordova plugin add Your_Plugin_Path  --variable API_KEY=your_jpush_appkey
  • github地址:https://github.com/jpush/jpush-phonegap-plugin

2.android端安装成功之后(如果安装失败,请更换不通的方法重新安装,尽量清理干净以前安装出错的)

app.js中添加一下两句代码,打包之后就可以进行推送消息了!

3.安卓端自定义通知的样式(重点来啦)

修改JPushPlugin.java 下的setCustomPushNotificationBuilder方法,如图

CustomPushNotificationBuilder(MainActivity.this,                              R.layout.customer_notitfication_layout,                              R.id.icon,                              R.id.title,                              R.id.text);                              // 指定定制的 Notification Layout    builder.statusBarDrawable = R.drawable.your_notification_icon;         // 指定最顶层状态栏小图标    builder.layoutIconDrawable = R.drawable.your_2_notification_icon;      // 指定下拉状态栏时显示的通知图标    JPushInterface.setPushNotificationBuilder(2, builder);
注意:your_notification_icon必须放在 android的res/drawable-hdpi下面,记得导入R文件哦 import  你的包名.R,customer_notitfication_layout样式布局,可以自定义哦;

然后需要在js中调用setCustomPushNotificationBuilder方法,如图


官网说明:https://docs.jiguang.cn/jpush/client/Android/android_senior/#_8

4.接收通知栏的消息

监听jpush.openNotification,如图

代码(方便复制):
document.addEventListener("jpush.openNotification", function(event){    var alertContent;    if(device.platform == "Android") {        alertContent = event.alert;    } else {        alertContent = event.aps.alert;    }    alert("open Notificaiton:" + alertContent);    }, false);
github地址:https://github.com/jpush/jpush-phonegap-plugin

0 0
原创粉丝点击