gpt4 book ai didi

在模拟器上使用 PushPlugin 的 Android PushNotifications

转载 作者:太空狗 更新时间:2023-10-29 16:15:37 30 4
gpt4 key购买 nike

我一直在关注 this sample让 android pushNotifications(使用 GCM)在 android 模拟器上工作。在 $cordovaPush.register(config) 之后我得到 Ok 作为响应。但它从不运行我的回调 [$scope.$on('$cordovaPush:notificationReceived']。结果我从来没有得到我的注册 ID。

我创建了一个 google API 项目。我在调用 $cordovaPush.register(config) 时在 config.senderID 中使用该项目 ID。

我还在我的模拟器中注册了一个 gmail 帐户。

我想我有 2 个问题。

1.是否可以在安卓模拟器上获取(并注册)推送通知?

  1. 为什么我没有收到调用回调的 $cordovaPush:notificationReceived 事件?

    app.controller('AppCtrl', function($scope, $cordovaPush, $cordovaDialogs, $cordovaMedia, $cordovaToast, ionPlatform, $http) { $scope.notifications = [];

    // call to register automatically upon device ready
    ionPlatform.ready.then(function (device) {
    $scope.register();
    });


    // Register
    $scope.register = function () {
    var config = null;

    if (ionic.Platform.isAndroid()) {
    config = {
    "senderID": "12834957xxxx"
    };
    }

    $cordovaPush.register(config).then(function (result) {
    console.log("Register success " + result);

    $cordovaToast.showShortCenter('Registered for push notifications');
    $scope.registerDisabled=true;

    }, function (err) {
    console.log("Register error " + err)
    });
    }


    $scope.$on('$cordovaPush:notificationReceived', function (event, notification) {
    console.log(JSON.stringify([notification]));
    if (ionic.Platform.isAndroid()) {
    handleAndroid(notification);
    }

    });

    // Android Notification Received Handler
    function handleAndroid(notification) {
    // ** NOTE: ** You could add code for when app is in foreground or not, or coming from coldstart here too
    // via the console fields as shown.
    console.log("In foreground " + notification.foreground + " Coldstart " + notification.coldstart);
    if (notification.event == "registered") {
    $scope.regId = notification.regid;
    storeDeviceToken("android");
    }
    else if (notification.event == "message") {
    $cordovaDialogs.alert(notification.message, "Push Notification Received");
    $scope.$apply(function () {
    $scope.notifications.push(JSON.stringify(notification.message));
    })
    }
    else if (notification.event == "error")
    $cordovaDialogs.alert(notification.msg, "Push notification error event");
    else $cordovaDialogs.alert(notification.event, "Push notification handler - Unprocessed Event");
    }

最佳答案

修复比看起来简单得多。我改变了:

$scope.$on('$cordovaPush:notificationReceived', function (event, notification) {

到:

$scope.$on('pushNotificationReceived', function (event, notification) {

同时 debugging我在 ng-cordova.js 上注意到了这一点:

angular.module('ngCordova.plugins.push', [])
.factory('$cordovaPush', ['$q', '$window', '$rootScope', function ($q, $window, $rootScope) {
return {
onNotification: function (notification) {
$rootScope.$apply(function () {
$rootScope.$broadcast('pushNotificationReceived', notification);
});
},

这意味着它正在为“pushNotificationReceived”进行广播。不是“notificationReceived”为 documented .

关于在模拟器上使用 PushPlugin 的 Android PushNotifications,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28677612/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com