Android
Push Notifications
通过Cloud Code实现Parse推送通知在Android中应用
15 分
使用云代码发送parse推送通知 介绍 本节解释了如何通过back4app使用云代码发送推送通知。 它的外观如下 您可以随时访问我们教程构建的完整android项目,链接在我们的 github仓库。 前提条件 要完成本教程,我们需要: android studio 在 back4app 上创建的应用程序。 注意: 请遵循 新 parse 应用程序教程 了解如何在 back4app 上创建 parse 应用程序。 一个连接到 back4app 的安卓应用。 注意: 请遵循 安装 parse sdk 教程 以创建一个连接到 back4app 的 android studio 项目。 请仔细遵循 步骤 1 到 5 的 通过仪表板的 back4app 推送通知教程 来设置推送通知到您的应用。 一个运行 api 级别 27 或更高版本的设备(或 虚拟设备 )。 1 设置 android 接收推送 每个安装在注册了推送通知的设备上的 parse 应用都有一个 关联的 安装 安装 对象。 安装 安装 对象是您存储所有需要的推送通知目标数据的地方。例如,在您的应用中,您可以存储您的用户感兴趣的团队,以便发送有关他们表现的更新。保存 安装 安装 对象也是跟踪与推送相关的应用打开事件所必需的。 开始发送通知的最简单方法是使用频道。这允许您使用发布 订阅模型来发送推送。设备首先订阅一个或多个频道,通知可以稍后发送给这些订阅者。给定的 安装 安装 订阅的频道存储在 频道 频道 字段的 安装 安装 对象中。 要开始使用推送通知,需要以下步骤: 如果您下载了我们的 项目模板 , 别忘了在 app/src/main/res/values/string xml app/src/main/res/values/string xml 文件和 gcmsenderid gcmsenderid 中更改您的凭据,该凭据是在 androidmanifest xml androidmanifest xml 文件中从 firebase 获取的 导入以下依赖项 1 // java 依赖项 2 import java util arraylist ; 3 // parse 依赖项 4 import com parse parse ; 5 import com parse parseinstallation ; 2\ 使用 parse initialize(this) parse initialize(this) 初始化 parse 3\ 创建一个新的频道数组,并放入您希望订阅的频道。在此示例中, news news 频道被创建 4\ 将您的 gcmsenderid gcmsenderid , 从 firebase 控制台 , 通过命令 installation put("gcmsenderid", "your firebase gcm sender id here") installation put("gcmsenderid", "your firebase gcm sender id here") 要了解如何获取该密钥,请查看 第 1 步 的 通过仪表板的推送通知教程 5\ 将 channels channels 对象添加到 installation installation 通过命令 installation put("channels", channels) installation put("channels", channels) 6\ 通过 installation saveinbackground() installation saveinbackground() 将安装保存到您的数据库中 以下代码执行这些步骤 1 parse initialize(this); 2 arraylist\<string> channels = new arraylist<>(); 3 channels add("news"); 4 parseinstallation installation = parseinstallation getcurrentinstallation(); 5 // don't forget to change the line below with the sender id you obtained at firebase 6 installation put("gcmsenderid", "your firebase gcm sender id here"); 7 installation put("channels", channels); 8 installation saveinbackground(); 2 创建您的云代码 要了解如何开始使用 cloud code 请查看 android 的 cloud code 教程 创建一个 js js 文件以放置您的 cloud code。在此示例中,创建了一个 main js main js 文件。 定义一个 cloud 函数,使用 parse cloud define parse cloud define , 来调用推送通知。在此示例中,此函数称为 parse push send parse push send 在此操作中需要使用 主密钥 以下代码执行这些步骤: parse server 3 x //main js 1 parse cloud define("pushsample", (request) => { 2 3 return parse push send({ 5 channels \["news"], 6 data { 7 title "hello from the cloud code", 8 alert "back4app rocks!", 9 } 10 }, { usemasterkey true }); 11 }); parse server 2 x //main js 1 parse cloud define("pushsample", function (request, response) { 2 parse push send({ 3 channels \["news"], 4 data { 5 title "hello from the cloud code", 6 alert "back4app rocks!", 7 } 8 }, { 9 success function () { 10 // push was successful 11 response success("push sent"); 12 console log("success push sent"); 13 }, 14 error function (error) { 15 // push was unsucessful 16 response error("error with push " + error); 17 console log("error " + error); 18 }, 19 usemasterkey true 20 }); 21 }); 3 上传到 cloud code 前往您的应用程序,位于 back4app 网站 并点击 仪表板 仪表板 找到 云代码 云代码 并点击 函数与网络托管 函数与网络托管 。它看起来像这样 3\ 上传或创建一个新文件(您也可以直接在浏览器中编辑当前的 main js main js 文件)。然后,点击 部署 部署 ,如下所示 4 从 android 应用程序调用云代码 导入以下依赖项 1 // java 依赖项 2 import java util hashmap ; // 这包括云函数需要调用的 hasmap 对象 3 // 解析依赖项 4 import com parse functioncallback ; 5 import com parse parsecloud ; 6 import com parse parseexception ; 2\ 调用 parsecloud callfunctioninbackground parsecloud callfunctioninbackground 在 pushsample pushsample 云函数 1 final hashmap\<string, string> params = new hashmap<>(); 2 // calling the cloud code function 3 parsecloud callfunctioninbackground("pushsample", params, new functioncallback\<object>() { 4 @override 5 public void done(object response, parseexception exc) { 6 if(exc == null) { 7 // the function was executed, but it's interesting to check its response 8 alertdisplayer("successful push","check on your phone the notifications to confirm!"); 9 } 10 else { 11 // something went wrong 12 toast maketext(mainactivity this, exc getmessage(), toast length long) show(); 13 } 14 } 15 }); 上面示例中使用的 alertdisplayer alertdisplayer 方法如下 1 private void alertdisplayer(string title ,string message ){ 2 alertdialog builder builder = newalertdialog builder (mainactivity this ) 3 settitle ( title ) 4 setmessage ( message ) 5 setpositivebutton ( "ok" , new dialoginterface onclicklistener () { 6 @override 7 public void onclick(dialoginterface dialog , int which ) { 8 dialog cancel (); 9 } 10 }); 11 alertdialog ok = builder create (); 12 ok show (); 13 } 3\ 测试推送通知是否被发送,可以在设备打开时调用上面的函数。 5 从 rest api 调用云代码 rest api 提供了一种快速简便的方法来测试您的云函数是否正常工作。 只需在终端或命令提示符中使用以下代码 点击了解如何在 linux , macos 或 windows curl x post h "x parse application id your app id here" \\ h "x parse rest api key your rest api key here" \\ h "content type application/json" \\ d '{ // 在此处以 json 格式放置函数参数 }' \\ https //parseapi back4app com/functions/pushsample 要测试推送通知,只需在设备打开时使用 rest 代码 完成了! 在这个阶段,您可以通过 back4app 使用云代码发送推送通知!