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

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方法,如图



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
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值