【发布时间】:2015-07-08 22:23:20
【问题描述】:
我正在使用 Ionic 框架构建一个推送通知应用程序,因此我尝试遵循这个示例应用程序:https://github.com/hollyschinsky/PushNotificationSample
问题在于,当我尝试在 Android 设备中运行示例时,它不会检索设备令牌。我将 register 函数中的 senderId 替换为我自己的 Google 应用程序中的 senderId,但它不起作用。
我做错了什么?
这是我正在使用的版本:
Ionic --version 1.3.20
Cordova --version 5.0.0
Phonegap --version 4.2.0-0.24.2
Android device: HTC One S
Android version: 4.1.1 HTC Sense 4+
我认为问题出在 notificationReceived 函数中:
// Register
$scope.register = function () {
var config = null;
if (ionic.Platform.isAndroid()) {
config = {
"senderID": "1034029444859" // REPLACE THIS WITH YOURS FROM GCM CONSOLE - also in the project URL like: https://console.developers.google.com/project/434205989073
};
//alert("El senderID es: " + senderID);
}
else if (ionic.Platform.isIOS()) {
config = {
"badge": "true",
"sound": "true",
"alert": "true"
}
}
$cordovaPush.register(config).then(function (result) {
console.log("Register success " + result);
$cordovaToast.showShortCenter('Registered for push notifications ' + config.senderID);
$scope.registerDisabled=true;
// ** NOTE: Android regid result comes back in the pushNotificationReceived, only iOS returned here
if (ionic.Platform.isIOS()) {
$scope.regId = result;
storeDeviceToken("ios");
}
}, function (err) {
console.log("Register error " + err)
});
}
// Notification Received
$scope.$on('$cordovaPush:notificationReceived', function (event, notification) {
console.log(JSON.stringify([notification]));
if (ionic.Platform.isAndroid()) {
handleAndroid(notification);
}
else if (ionic.Platform.isIOS()) {
handleIOS(notification);
$scope.$apply(function () {
$scope.notifications.push(JSON.stringify(notification.alert));
})
}
});
编辑:
事实证明,该应用确实检索了设备令牌,只是没有按应有的方式在屏幕上显示它。
我必须在通过 USB 和 ionic run android 连接的设备上运行应用程序,并查看应用程序如何使用 adb logcat 运行,然后我看到一行显示“registrationId = APA91b ...”这就是令牌.我按照教程中的说明使用 sendGCM.js 文件测试了通知,它可以正常工作。
但它仍然没有解释为什么它没有在索引页面中显示令牌或收到的通知。就像 Token 总是超出控制器的范围。
有什么帮助吗?
【问题讨论】:
-
你能 console.log(JSON.stringify(result)) 吗?你得到了什么?
-
我尝试使用 'ionic emulate android --livereload' 运行模拟器以查看 console.log 并显示“出现网络错误 (172.16.16.50:8100)”。有什么建议吗?
-
嗯,我认为推送通知功能应该在真实设备而不是模拟器上进行测试。
-
@David 我也在这里找到了相同的点,但是您是如何检索 ID 并将其提交到您的服务器的?
-
@Emm 我将设备连接到 PC 并运行 adb logcat 进行调试。注册函数的结果就埋在那里。仅供参考,我放弃了这个示例,并根据文档中的示例(ngcordova.com/docs/plugins/pushNotifications)制作了自己的推送通知功能。推送通知在 Android 模拟器中确实有效,但在 iOS 模拟器中无效。
标签: android cordova push-notification ionic-framework cordova-plugins