从 Drupal 站点发送推送通知。






4.95/5 (15投票s)
如何将推送通知集成到您的 Drupal 7 站点和 Titanium 应用中。
引言
本文我将分享如何通过 Apple 和 Android 的通知服务集成您 Drupal 网站的推送通知管理。
在此场景中,您在 Apple(Apple Store)和 Google(Google Play)应用市场拥有已部署的个人或企业应用,并且希望从您的 Drupal 网站向这两个应用的用户发送推送通知。
在本示例中,应用程序是使用 Titanium Appcelerator 开发的,因此本文提供的解决方案中描述的应用程序代码将符合此要求。如果您使用的是原生代码(iOS 使用 Objective C,Android 使用 Java),只需根据原生 API 调整涉及客户端应用程序的步骤即可。
假设您了解如何管理 Drupal 7 网站,包括安装模块等。
背景
以下是此集成中使用的技术的有用链接:
Drupal: https://drupal.org/
- Titanium Mobile 开发环境: http://www.appcelerator.com/titanium/
- Push Notifications Drupal 模块: https://drupal.org/project/push_notifications
- Services Drupal 模块: https://drupal.org/project/services
- Libraries Drupal 模块: https://www.drupal.org/project/libraries
- Google Cloud Messaging for Android: https://developer.android.com.cn/google/gcm/index.html
- Apple Push Notification Service: https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/ApplePushService.html
架构
要求
iOS 开发者计划帐户。这是必需的,以便
- 生成提供商(您的 Drupal 7 网站)用于将通知发送到 APNs 的证书
- 生成用于分发将接收通知的应用程序的证书、应用程序 ID 和预配配置文件。
Google 帐户。这是必需的,以便
- 创建 Google API 项目(生成项目 ID 和项目编号)
- 为该项目启用 GCM(Google Cloud Messaging)服务
- 获取服务器 API 密钥
一个已安装并配置了一些模块的 Drupal 7 网站
- Push Notifications 模块: https://www.drupal.org/project/push_notifications
- Services 模块: https://www.drupal.org/project/services
- Libraries 模块: https://www.drupal.org/project/libraries
描述
在此场景中,您拥有在 Apple 和 Google 应用市场(Apple Store 和 Google Play)部署的、使用 Appcelerator Titanium 开发的个人或企业应用,并且希望从您的 Drupal 网站向这两个应用的用户发送推送通知。
Drupal: 步骤
- 安装 Services 模块,访问 https://www.drupal.org/project/services,下载包并手动安装,或者在 Drupal 网站管理界面的“模块”部分使用安装包的实用程序(安装新模块)。此模块需要 Chaos Tools 模块,因此如果尚未安装,请先安装。 https://www.drupal.org/project/ctools
- 通过 Drupal 的模块管理界面启用 Services 模块。
- 安装并启用 Push Notifications 模块,访问 https://www.drupal.org/project/push_notifications,下载包并手动安装,或者在 Drupal 网站管理界面的“模块”部分使用安装包的实用程序(安装新模块)。
- 通过 Drupal 的模块管理界面启用 Push Notifications 模块。
- 安装并启用 Libraries 模块,访问 https://www.drupal.org/project/libraries,下载包并手动安装,或者在 Drupal 网站管理界面的“模块”部分使用安装包的实用程序(安装新模块)。
- 通过 Drupal 的模块管理界面启用 Libraries 模块。
- 安装 Services 模块后,另一个模块会一起安装,即 REST Server 模块。您需要做的就是通过 Drupal 的模块管理界面也启用此模块。
- 现在您需要创建一个 http 端点,用于创建和删除将存储和用于发送推送通知的设备令牌。例如,如果您的网站部署在 https://www.mysite.com,并且您想调用名为“myendpoint”的端点,请执行以下操作:
- 转到 Drupal 模块管理页面。
- 点击 Services 模块的“配置”链接。
- 点击“添加”链接(添加新端点),将显示一个表单。
- 填写表单:
- 端点的机器可读名称:输入“myendpoint”
- 服务器:选择“REST”
- 端点路径:输入“myendpoint”
- 点击“保存”按钮。
当端点出现在端点列表中时,配置要附加到此端点的推送通知资源。为此:
- 点击“编辑资源”链接。
- 在资源列表中,选中“push_notifications”资源的复选框,该资源的所有方法(创建和删除)也将被选中。
如果您的网站部署在 https://www.mysite.com,并且端点已命名为“myendpoint”,则可以通过 https://www.mysite.com/myendpoint/push_notifications 访问这些资源,并且您需要:
- 执行 POST 请求以创建新的设备令牌。
- 执行 DELETE 请求以删除现有设备令牌。
iOS 通知后端配置:步骤
在 iOS 环境中处理推送通知最重要的是正确生成证书和预配配置文件。您需要两个证书:
一个用于应用程序,用于分发 Apple Store 中的应用程序,并用于生成分发预配配置文件。要了解更多关于客户端证书、应用程序 ID、设备和预配配置文件(所有这些对于客户端应用程序都是必需的),您必须了解 iOS 开发者平台的工作原理,这里是摘要:
- 生成证书(用于开发环境的 iOS App Development 证书,或用于生产环境的 Distribution / AdHoc 证书)。
- 为您的应用程序注册一个 iOS App ID 标识符,并启用推送通知服务。
- 如果您在开发环境工作并想分发应用程序进行测试,则必须通过添加测试设备 UUID 来启用用户设备。
- 生成一个预配配置文件,该配置文件将一个证书和一个 App ID 附加到一个必需的文件中,以便分发 App。
一个用于服务器,您需要另一个证书供推送通知服务器提供商(您的 Drupal 网站,在 Apache 下运行)使用。以下步骤将帮助您生成开发证书,生产证书的步骤相同,但选择分发或生产项目。所有这些步骤都必须在 MAC OS 计算机上完成。
- 转到 iOS 开发者计划网页,登录并访问“证书、标识符和配置文件”部分。
- 添加新证书 - 开发 - 并选择“Apple Push Notification service SSL (Sandbox)”选项。点击“继续”按钮。
- 选择将通过此证书接收推送通知的应用程序的 App ID。点击“继续”按钮。
- 要手动生成证书,您需要 Mac 上的证书签名请求(CSR)文件。要创建 CSR 文件,请按照页面上的说明使用 Keychain Access 创建一个。
- 上传在 MAC 计算机上保存的 CSR 文件。
- 将证书下载到您的 Mac,然后双击 .cer 文件以安装到 Keychain Access(以前称为 apns_development.cer)。
- 将该证书(.cer)导出为 .p12。
要将证书导出为 .p12,需要拥有用于生成证书的私钥(当您首次生成证书时,您必须先使用 Keychain - 步骤 4 和 5 - 创建 CSR(证书签名请求)。这会将私钥放入您的 Keychain。然后您将 CSR 提交给 Apple Developer Portal,它会将证书返还给您)。
在 Keychain Access 工具中,选择“我的证书”类别,然后选择证书和私钥 - 两者都要!这非常重要 - 然后右键单击以显示菜单,点击“导出 2 项”。
- 将 .p12 证书转换为 .pem:在此步骤中,您需要安装 openssl 实用程序,如果它尚未安装在您的操作系统(Linux、Mac)中。转换命令为:
openssl pkcs12 -in apns_development.p12 -out apns_development.pem -clcerts -nodes
- 将 .pem 文件部署到您 Drupal 网站 Push Notifications 模块的“certificates”文件夹(这是模块的默认配置)。例如:/var/www/yoursite/sites/all/modules/push_notifications/certificates
Titanium - iOS 应用程序
在此情况下,我们使用 Titanium Mobile 开发环境 http://www.appcelerator.com/titanium 开发了 iOS 应用程序。
目前还没有太多针对 Titanium 的模块能够同时管理推送通知服务 GCM(Google Cloud Messaging for Android apps)和 APN(Apple Push Notifications)。目前最好的解决方案是使用 Titanium Network API 来管理 iOS 推送通知,并为 Android 推送通知使用一个外部模块。
配置和管理 iOS Titanium App 中推送通知的步骤如下:
创建一个 JavaScript 模块(例如,m_push_notifications.js),其中包含一个 public
方法,当我们要初始化推送通知时将调用该方法。此初始化涉及:
- 注册应用程序以接收推送通知。这将调用 APNs 服务,该服务将为我们提供设备令牌。设备令牌类似于电话号码;它包含使 APNs 能够定位安装了客户端应用程序的设备的信息。APNs 还使用它来验证通知的路由。
- 实现将管理注册后收到的设备令牌的回调。在这种情况下,我们必须调用我们的 Drupal 后端 API 以在提供商处注册设备令牌。
- 实现管理设备令牌错误接收的回调。
- 实现管理推送通知消息接收的回调。我们将消息接收为一个 JavaScript 对象。
exports.initPushNotifications = function() { if(Alloy.isIos) { var iosDeviceToken = null; Ti.Network.registerForPushNotifications({ // Specifies which notifications to receive types: [ Ti.Network.NOTIFICATION_TYPE_BADGE, Ti.Network.NOTIFICATION_TYPE_ALERT, Ti.Network.NOTIFICATION_TYPE_SOUND ], success: iosDeviceTokenSuccess, error: iosDeviceTokenError, callback: iosReceivePush }); function iosReceivePush(e) { customAlert(e.data.aps.alert); } // Save the device token on the backend (Push Provider) function iosDeviceTokenSuccess(e) { // Compose the Payload to be sent to the PUSH Provider payload ={ token : e.deviceToken, type : 'ios' }; // Call client = Ti.Network.createHTTPClient({ // function called when the response data is available onload : function(e) { console.log("OK : " + e.text); }, // function called when an error occurs, including a timeout onerror : function(e) { console.log("ERROR : " + e.error); }, timeout : 3000, // in milliseconds // If it's an https connection, to validate or not the certificate validatesSecureCertificate: Alloy.CFG.VALIDATE_SECURE_CERTIFICATE }); client.setRequestHeader("Content-Type", contentType); // Prepare the connection. client.open("POST", 'http://yourserverurl/endpoint/push_notifications'); // Send the request. client.send(payload); } function iosDeviceTokenError(e) { customAlert('Failed to register for iOS push notifications! ' + e.error); } } }
Android 通知后端配置:步骤
在这种情况下,这非常简单。您只需要有一个 Google 帐户,为您的应用程序创建一个项目,激活 GCM 服务,并创建一个 API 密钥供您的 Drupal 的 Push Notification 模块配置使用。请遵循以下步骤:
1)创建项目
- 登录您的 Google 帐户。
- 转到 Google 开发者控制台,https://console.developers.google.com/
- 点击“创建项目”按钮。
- 填写所需数据:项目名称和项目 ID。
- 点击“创建”按钮。这将生成项目并重定向到项目仪表板页面。
2)为 Android 激活 GCM 服务
- 在项目仪表板中,点击“APIs & auth”菜单下的“APIs”链接。
- 这将显示一个 Google API 列表(已启用或未启用)。搜索“Google Cloud Messaging for Android”服务,然后点击“OFF”按钮将其启用。
3)为您的服务器创建 API 密钥
- 在项目仪表板中,点击“APIs & auth”菜单下的“Credentials”链接。
- 点击“创建新密钥”按钮。
- 点击“服务器密钥”按钮。选择接收请求的 IP 范围或地址(您的 Drupal 服务器的公共 IP,如果希望从任何地方使用此服务则留空)。
- 点击“创建”按钮。
- 将显示服务器密钥信息。选择生成的 API KEY 并复制。
4)配置您的 Drupal 的 Push Notifications 模块以使用此 API KEY
- 转到 Drupal 模块管理页面。
- 点击 Push Notifications 模块的“配置”链接。
- 点击“配置”选项卡。
- 在 GOOGLE CLOUD MESSAGING 部分,在“Google Cloud Messaging API Key”文本框中填入上一步提供的密钥。
- 点击“保存配置”按钮。
完成了!就是这么简单。
Titanium - Android 应用程序
在集成使用 Titanium 的 Android 平台推送通知的情况下,我们需要一个外部模块来解决这个问题。我推荐两个模块:
- IamYellow 的 GCM Push Notifications Module for Titanium (https://github.com/iamyellow/gcm.js),http://iamyellow.net/post/40100981563/gcm-appcelerator-titanium-module
- Liccowee 的 GCM in titanium: https://github.com/liccowee/Google-Cloud-Messaging--Titanium-
此处解释的所有代码均使用第二个模块(Liccowee 的)。
配置和管理 Android Titanium App 中推送通知的步骤是:在创建的 JavaScript 模块 m_push_notifications.js 中,在创建的用于 init
推送通知的 public
方法中添加管理 Android 推送通知的代码。这包括:
- 配置 tiapp.xml 配置文件以启用 GCM 推送通知。
<property name="com.activate.gcm.sender_id" type="string">XXXXXXXXX</property> <property name="com.activate.gcm.icon" type="int">2130837504</property> <property name="com.activate.gcm.component" type="string"> com.entelgy.telemadrid.haztutele/com.entelgy.telemadrid.haztutele.HaztuteleActivity </property>
属性
sender_id
的值(在此例中为 XXXXXXXXXX)必须是 Google 开发者控制台中生成的项目的项目 ID 值。 - 导入 gcm 模块。
- 注册应用程序以接收推送通知。这将调用 GCM 服务,该服务将为我们提供注册 ID。
- 实现将管理注册后收到的注册 ID 的回调。在这种情况下,我们必须调用我们的 Drupal 后端 API 以在提供商处注册设备令牌。
- 实现管理注册 ID 错误接收的回调。
- 实现管理推送通知消息接收的回调。我们将消息接收为一个 JavaScript 对象。
gcm = require('com.activate.gcm'); if(Alloy.isAndroid){ var hostURL = 'http://urlmyhost/endpoint/push_notifications'; gcm.registerC2dm({ success:function(e){ var regId = e.registrationId; var payload = { token : e.registrationId, type : 'android' }; client = Ti.Network.createHTTPClient({ onload : function(e) { Ti.API.info("Register device token in CMS success"); }, // function called when an error occurs, including a timeout onerror : function(e) { Ti.API.error("Register device token in CMS error"); }, timeout : 3000, // in milliseconds validatesSecureCertificate: Alloy.CFG.VALIDATE_SECURE_CERTIFICATE }); client.setRequestHeader("Content-Type", contentType); client.open("POST", urlPushProvider); client.send(payload); }, error:function(e){ Ti.API.error("Error during registration : " + e.error); var message; if(e.error == "ACCOUNT_MISSING"){ message = "No Google account found; you will need to add on in order to activate notifications"; } else{ message = e.error; } }, // Callback executed when a push notification has been received callback : function ( e ) { processAndroidNotification(e); } } ); }
历史
- 2015 年 5 月 6 日:初始版本