使用 Ionic 框架推送通知
Push Notifications with Ionic Framework
我正在尝试使用 Ionic 实现推送通知,但我很困惑。到目前为止,我已经尝试了以下替代方法但没有成功:
离子推
似乎是最好的解决方案。它 "simulates" 浏览器中的通知使测试变得容易。有两个版本:
版本 1.0:文档可用 http://docs.ionic.io/v1.0/docs/push-overview. The page displays an warning at the bottom saying to check the latest version (2.0beta). If I keep in the page and try to follow the instructions, everything seems to work fine, but if I send the push to https://push.ionic.io/api/v1/push 没有消息到达。查看 firebug 我发现 $ionicPush 正在查询 2.0alpha API 而不是查询 1.0 API.
2.0 版:文档可在 http://docs.ionic.io/docs/push-overview 获得。它与 1.0 非常相似,并且在浏览器、Android(开发和生产)和 iOS 开发环境中运行良好。但是,我无法收到关于 IOS 生产的消息(在将应用发布到商店之后)。我努力寻找解决方案,但我发现一些答案说它是 alpha,不应该使用。
因此,如果版本 1.0 不存在(它正在使用 v2.0 API)并且 v2.0 是 alpha 并且不应使用,我断定 Ionic Push 无法使用。
问题 1:是否可以使用 ionic push 向 iOS 和 Android 发送通知?
问题 2:使用 2.0 设置,我可以使用 Apns PHP 而不是 Ionic Push 发送消息吗?
Cordova Phonegap 插件推送
我猜这个插件是 Ionic Push 在幕后使用的。但是,我无法弄清楚如何使用它。我发现的教程和文档已过时并引用了一个已弃用的版本(https://github.com/phonegap-build/PushPlugin.git). Even ngCordova references this deprecated version (http://ngcordova.com/docs/plugins/pushNotifications/). ngCordova suggests Ionic Push also. I can't find an full example showing how to use https://github.com/phonegap/phonegap-plugin-push with ionic.
问题3:如何使用ionic设置Cordova Phonegap Plugin Push?
我还没有用过Ionic Push,只能尝试回答你的第三个问题
实际上,我认为 GitHub 插件页面上的示例非常好,所以我将参考这些示例。如果大家还有什么问题可以留言,我会详细说明的。
首先,将 example script 包含在 deviceready
侦听器回调中的 index.html 中,并使用您的设置初始化插件。在注册事件处理程序中,您可以通过 data.registrationId
获取当前用户的设备 ID,您可以将其保存在数据库中。
要实际发送推送消息,我使用 node-gcm (Android) and apnagent (iOS) server-sided. There are also some good example at least for Android on the plugin page。
我已经为此苦苦挣扎了几天,直到我发现这个指南似乎真的很有用:
https://github.com/yafraorg/yafra/wiki/Blog-Ionic-PushV5
希望对其他人也有帮助!
这是 cordovaPushv5 的非官方文档,请在此处查看实际实现:https://github.com/driftyco/ng-cordova/blob/master/src/plugins/push_v5.js
我能够使用 Google Firebase 和 https://github.com/arnesson/cordova-plugin-firebase/ 通过 Ionic 1 send/receive 在 IOS 和 Android 上推送通知。
按照我所做的:
- 在 developer.apple.com 上创建了 DEV/PROD 配置文件。创建配置文件时检查了我的 developer/team 证书。
- 在 developer.apple.com 上创建了 DEV/PROD 个 APS 证书。
- 已在开发机器上安装 developer/team 个证书。
- 已在开发机器上安装 DEV/PROD APS 证书。
- 已在开发机器上安装 provisionig 配置文件。
- 已安装插件"cordova plugin add cordova-plugin-firebase@0.1.14"。
向我的应用程序添加了代码:
window.FirebasePlugin.grantPermission();
window.FirebasePlugin.getInstanceId(
函数(令牌){
thiss.saveToken(令牌);
},
功能(错误){
console.log(错误);
}
);
window.FirebasePlugin.onNotificationOpen(
功能(通知){
//警报("Yeahh!");
},
功能(错误){
//警报("Error");
}
);
已创建 Google GCM/Google Firebase 帐户。
- 将我的应用添加到 iOS/Android 平台的 Firebase。
- 在 firebase 项目设置的 "Cloud Messaging" 部分添加了 APS 证书。
- 下载 GoogleService-Info.plist e google-service.json 并将它们放在我的应用程序根文件夹中。
- 构建应用程序。
仅 - IOS:在 XCode 中启用 "Push Notifications" 和 "Background Modes: Receive notifications".
要发送通知,我使用以下 php 脚本:
$data = Array
(
[to] => <token>
[priority] => high
[notification] => Array
(
[title] => My Title
[text] => Notification test
[sound] => default
[vibrate] => 1
[badge] => 0
)
)
$jsonData = json_encode($data);
$ch = curl_init("https://fcm.googleapis.com/fcm/send");
$header = array(
'Content-Type: application/json',
"Authorization: key=".$gcmApiKey
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, true );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
我正在尝试使用 Ionic 实现推送通知,但我很困惑。到目前为止,我已经尝试了以下替代方法但没有成功:
离子推
似乎是最好的解决方案。它 "simulates" 浏览器中的通知使测试变得容易。有两个版本:
版本 1.0:文档可用 http://docs.ionic.io/v1.0/docs/push-overview. The page displays an warning at the bottom saying to check the latest version (2.0beta). If I keep in the page and try to follow the instructions, everything seems to work fine, but if I send the push to https://push.ionic.io/api/v1/push 没有消息到达。查看 firebug 我发现 $ionicPush 正在查询 2.0alpha API 而不是查询 1.0 API.
2.0 版:文档可在 http://docs.ionic.io/docs/push-overview 获得。它与 1.0 非常相似,并且在浏览器、Android(开发和生产)和 iOS 开发环境中运行良好。但是,我无法收到关于 IOS 生产的消息(在将应用发布到商店之后)。我努力寻找解决方案,但我发现一些答案说它是 alpha,不应该使用。
因此,如果版本 1.0 不存在(它正在使用 v2.0 API)并且 v2.0 是 alpha 并且不应使用,我断定 Ionic Push 无法使用。
问题 1:是否可以使用 ionic push 向 iOS 和 Android 发送通知?
问题 2:使用 2.0 设置,我可以使用 Apns PHP 而不是 Ionic Push 发送消息吗?
Cordova Phonegap 插件推送
我猜这个插件是 Ionic Push 在幕后使用的。但是,我无法弄清楚如何使用它。我发现的教程和文档已过时并引用了一个已弃用的版本(https://github.com/phonegap-build/PushPlugin.git). Even ngCordova references this deprecated version (http://ngcordova.com/docs/plugins/pushNotifications/). ngCordova suggests Ionic Push also. I can't find an full example showing how to use https://github.com/phonegap/phonegap-plugin-push with ionic.
问题3:如何使用ionic设置Cordova Phonegap Plugin Push?
我还没有用过Ionic Push,只能尝试回答你的第三个问题
实际上,我认为 GitHub 插件页面上的示例非常好,所以我将参考这些示例。如果大家还有什么问题可以留言,我会详细说明的。
首先,将 example script 包含在 deviceready
侦听器回调中的 index.html 中,并使用您的设置初始化插件。在注册事件处理程序中,您可以通过 data.registrationId
获取当前用户的设备 ID,您可以将其保存在数据库中。
要实际发送推送消息,我使用 node-gcm (Android) and apnagent (iOS) server-sided. There are also some good example at least for Android on the plugin page。
我已经为此苦苦挣扎了几天,直到我发现这个指南似乎真的很有用: https://github.com/yafraorg/yafra/wiki/Blog-Ionic-PushV5
希望对其他人也有帮助!
这是 cordovaPushv5 的非官方文档,请在此处查看实际实现:https://github.com/driftyco/ng-cordova/blob/master/src/plugins/push_v5.js
我能够使用 Google Firebase 和 https://github.com/arnesson/cordova-plugin-firebase/ 通过 Ionic 1 send/receive 在 IOS 和 Android 上推送通知。 按照我所做的:
- 在 developer.apple.com 上创建了 DEV/PROD 配置文件。创建配置文件时检查了我的 developer/team 证书。
- 在 developer.apple.com 上创建了 DEV/PROD 个 APS 证书。
- 已在开发机器上安装 developer/team 个证书。
- 已在开发机器上安装 DEV/PROD APS 证书。
- 已在开发机器上安装 provisionig 配置文件。
- 已安装插件"cordova plugin add cordova-plugin-firebase@0.1.14"。
向我的应用程序添加了代码:
window.FirebasePlugin.grantPermission(); window.FirebasePlugin.getInstanceId( 函数(令牌){ thiss.saveToken(令牌); }, 功能(错误){ console.log(错误); } );
window.FirebasePlugin.onNotificationOpen( 功能(通知){
//警报("Yeahh!"); }, 功能(错误){ //警报("Error"); } );已创建 Google GCM/Google Firebase 帐户。
- 将我的应用添加到 iOS/Android 平台的 Firebase。
- 在 firebase 项目设置的 "Cloud Messaging" 部分添加了 APS 证书。
- 下载 GoogleService-Info.plist e google-service.json 并将它们放在我的应用程序根文件夹中。
- 构建应用程序。 仅
- IOS:在 XCode 中启用 "Push Notifications" 和 "Background Modes: Receive notifications".
要发送通知,我使用以下 php 脚本:
$data = Array
(
[to] => <token>
[priority] => high
[notification] => Array
(
[title] => My Title
[text] => Notification test
[sound] => default
[vibrate] => 1
[badge] => 0
)
)
$jsonData = json_encode($data);
$ch = curl_init("https://fcm.googleapis.com/fcm/send");
$header = array(
'Content-Type: application/json',
"Authorization: key=".$gcmApiKey
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, true );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
$result = curl_exec($ch);
curl_close($ch);
echo $result;