美文网首页
cordova vue 极光推送安卓

cordova vue 极光推送安卓

作者: 正在成长的切图仔 | 来源:发表于2021-08-30 16:16 被阅读0次
  1. 在cordova 目录安装cordova plugin add jpush-phonegap-plugin --variable APP_KEY=
    APP_KEY需要在极光推送哪里设定
    image.png
  2. 在public 目录下新建index.js js代码如下
  3. 吧index.js代码在index.html中引入(亲测有效)
var _t = this;
var onDeviceReadyS = function() {
    document.addEventListener("jpush.receiveRegistrationId", function (event) {
        alert("receiveRegistrationId" + JSON.stringify(event));
    }, false)
  initiateUI();
};
// 获得id
var getRegistrationID = function() {
    window.JPush.getRegistrationID(onGetRegistrationID);
};
var onGetRegistrationID = function(data) {
    try {
        console.log("JPushPlugin:registrationID is " + data);
        if (data.length == 0) {
            var t1 = window.setTimeout(getRegistrationID, 1000);
        }
    } catch (exception) {
        console.log(exception);
    }
};

var onTagsWithAlias = function(event) {
    try {
        console.log("onTagsWithAlias");
        var result = "result code:" + event.resultCode + " ";
        result += "tags:" + event.tags + " ";
        result += "alias:" + event.alias + " ";
        // $("#tagAliasResult").html(result);
    } catch (exception) {
        console.log(exception)
    }
};

var badgeNumb = 0;
var onOpenNotificationM = function(event) {
    try {
        var alertContent;
        if (device.platform == "Android") {
            alertContent = event.alert;
        } else {
            alertContent = event.aps.alert;
        }

        badgeNumb = badgeNumb - 1;
        badgeNumb = badgeNumb<=0 ? 0 : badgeNumb;
        window.JPush.setBadgeNumber(badgeNumb);
        alert("请及时查看:" + alertContent);
    } catch (exception) {
        // alert("JPushPlugin:onOpenNotificationM" + exception);
    }
};

var onReceiveNotificationX = function(event) {
    try {
        var alertContent;
        if (device.platform == "Android") {
            alertContent = event.alert;
        } else {
            alertContent = event.aps.alert;
        }
        // $("#notificationResult").html(alertContent);

        badgeNumb = badgeNumb + 1;
        window.JPush.setBadgeNumber(badgeNumb);
    } catch (exception) {
        console.log(exception)
    }
};

var onReceiveMessageZ = function(event) {
  try {
    var message;
    if (device.platform == "Android") {
      message = event.message;
    } else {
      message = event.content;
    }
    badgeNumb = badgeNumb + 1;
    window.JPush.setBadgeNumber(badgeNumb);
  } catch (exception) {
    console.log("JPushPlugin:onReceiveMessageZ-->" + exception);
  }
};

var onResumeS = function(event){
  try {
        badgeNumb = 0
        window.JPush.setBadgeNumber(0);
    } catch (exception) {
        console.log("onResumeS-->" + exception);
    }
}
// 初始化
var initiateUI = function() {
  try {
    // alert("window.JPush1");
    // window.setTimeout(()=>{// alert(window.JPush);}, 1000);
    // 如果报init()为null的错那就卸了你的推送插件重新安装
    // cordova plugin add jpush-phonegap-plugin --variable APP_KEY=你的极光推送的key
    window.JPush.init();
    window.JPush.setDebugMode(true);
    window.setTimeout(getRegistrationID, 1000);
    // 标签的
    setTags()
    getAllTags()
    // cleanTags()
    // 别名的
    setAlias()
    getAlias()
    // deleteAlias()

    if (device.platform != "Android") {
      window.JPush.setApplicationIconBadgeNumber(0);
    }
  } catch (exception) {
    console.log(exception);
    // alert(exception);
  }
};

// 设置tage
var setTags = function () {
  // alert("设置tage")
  try {
    //你可以设置多个tag  
    var tag1 = "1"
    var tag2 = "2"
    var tag3 = "3"
    var tags = []
    if (tag1) {
      tags.push(tag1)
    }
    if (tag2) {
      tags.push(tag2)
    }
    if (tag3) {
      tags.push(tag3)
    }

    window.JPush.setTags({ sequence: 1, tags: tags },
      function (result) {
        // 设置成功时
        // JSON.stringify(result.tags)
        // alert(JSON.stringify(result.tags))
        //  清除
        // _t.cleanTags()
      }, function (error) {
        // alert(error.code)
      })
  } catch (exception) {
    console.log(exception)
  }
}

// 获取tage
var getAllTags = function () {
  // alert("获取tage")
  window.JPush.getAllTags({ sequence: 2 },
    function (result) {
      // 获取标签成功
      // alert(JSON.stringify(result.tags))
      //  清除
      // _t.deleteAlias()
    }, function (error) {
      // alert(error.code)
    })
}
// 清空tage
var cleanTags = function () {
  // alert("清空tage")
    window.JPush.cleanTags({ sequence: 2 },
      function (result) {
        // alert(result.sequence)
      }, function (error) {
        // alert(error.code)
      }
    )
}
// 设置设备别名
var setAlias = function () {
  // alert("设置别名")
    var alias = "122223"
    window.JPush.setAlias({ sequence: 1, alias: alias },
    function (result) {
      // 成功后返回别名
      // result.alias
      // alert(result.alias)
    }, function (error){
      // alert(error.code)
    })
}
// 获取设备别名
var getAlias = function () {
  // alert("获取设备别名")
  window.JPush.getAlias({ sequence: 2 },
    function (result) {
      // 获取成功
      // alert(JSON.stringify(result));
    }, function (error) {
      // alert(error.code)
    }
  )
}
// 删除设备别名
var deleteAlias = function () {
    window.JPush.deleteAlias({ sequence: 3 },
      function (result) {
        // 删除成功
        // alert(JSON.stringify(result));
      }, function (error) {
        // alert(error.code)
      }
    )
}
  
// 设备加载成功的时候 
document.addEventListener("deviceready", onDeviceReadyS, false);
// 获取点击状态栏中的通知内容时
document.addEventListener("jpush.openNotification", onOpenNotificationM, false);
// 收到消息   
document.addEventListener("jpush.receiveNotification", onReceiveNotificationX, false);
// 获取自定义消息
document.addEventListener("jpush.receiveMessage", onReceiveMessageZ, false);
// 处理恢复事件
document.addEventListener("resume", onResumeS, false);


相关文章

网友评论

      本文标题:cordova vue 极光推送安卓

      本文链接:https://www.haomeiwen.com/subject/lqroiltx.html