Cordova 推送通知 (PhoneGap PushPlugin) 未触发 ecb 回调 (onNotificationAPN)

Cordova Push Notifications (PhoneGap PushPlugin) not firing ecb callback (onNotificationAPN)

我将 Cordova Push Notifications Plugin 1.3.4 与我的 Cordova/Phonegap 应用程序一起使用。不幸的是,当收到推送通知时,我的 JavaScript 中的 ecb 回调从未被触发,我无法处理推送通知(即使应用程序在前台运行时也无法处理)。

我正在使用演示中的示例代码:

1
pushNotification.register(tokenHandler, errorHandler, {"badge":"true","sound":"true","alert":"true","ecb":"onNotificationAPN"});

注册成功,但从未触发以下回调:

1
2
3
4
5
6
function onNotificationAPN (event) {
    if (event.alert)
    {
        navigator.notification.alert(event.alert);
    }
 }

问题在于您定义回调函数的方式,导致推送插件评估您的回调(即通过 [webView stringByEvaluatingJavaScriptFromString)失败,因为它不会意识到这一点。

如果您将回调函数定义为全局对象,插件将在每次新通知到达时正确触发您的回调:

1
2
3
4
5
6
onNotificationAPN = function(event) {
    if (event.alert)
    {
        navigator.notification.alert(event.alert);
    };
};

对于 Android,您会以同样的方式定义您的 onNotificationGCM 回调。


Mobiletainment 的回答为我解决了这个问题!我整个下午都在寻找解决方案,尽管是 GCM 回调,但功能是相同的。

我会支持 Mobiletainment,但我是新来的,它不会让我这样做,但是谢谢!

对于任何可能正在寻找 GCM 解决方案(Googleverse 中有很多但没有很多好的答案)的人来说,我的代码终于奏效了:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// handle GCM notifications for Android
            onNotificationGCM = function(e){
                alert('onGCM');
                switch( e.event )
                {
                    case 'registered':
                        if ( e.regid.length > 0 )
                        {
                            console.log("Regid" + e.regid);
                            //Do registration work here...
                        }
                    break;

                    case 'message':
                      // this is the actual push notification. its format depends on the data model from the push server
                      alert('message = '+e.message+' msgcnt = '+e.msgcnt);
                    break;

                    case 'error':
                      alert('GCM error = '+e.msg);
                    break;

                    default:
                      alert('An unknown GCM event has occurred');
                      break;
                }
            }

如果你使用 azure push..below 是工作代码。(pushplugin for phonegap)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
    function InitpushNotificaions() {
      //  alert("Notification setup");
        try {


            mobileServiceClient = new WindowsAzure.MobileServiceClient('https://mobbacktest.azure-mobile.net/', 'RvahPxHKoEsGiLdlCYZpHBllvSVQxl66');
            pushNotification = window.plugins.pushNotification;
            if (device.platform == 'android' || device.platform == 'Android' ||
                    device.platform == 'amazon-fireos')
            {
                // AndroId

                pushNotification.register(successHandler, errorHandler, {"senderID":"724086851305","ecb":"onNotification" });       // required!
            }
            else
            {
                //IOS
                pushNotification.register(tokenHandler, errorHandler, {"badge":"true","sound":"true","alert":"true","ecb":"onNotificationAPN" });   // required!
            }
        }
        catch (err) {
            txt ="There was an error on this page.\
\
";
            txt +="Error description:" + err.message +"\
\
";
            alert(txt);
        }




    }


    // handle APNS notifications for iOS
 onNotificationAPN=function (e) {
        if (e.alert) {
            $("#app-status-ul").append('
<li>
push-notification: ' + e.alert + '
</li>
');
            // showing an alert also requires the org.apache.cordova.dialogs plugin
            navigator.notification.alert(e.alert);
        }

        if (e.sound) {
            // playing a sound also requires the org.apache.cordova.media plugin
            var snd = new Media(e.sound);
            snd.play();
        }

        if (e.badge) {
            pushNotification.setApplicationIconBadgeNumber(successHandler, e.badge);
        }
    }

    //handle GCM notifications for Android
    onNotification = function (e) {
       // alert("gcm");
        switch (e.event) {
            case 'registered':
                if (e.regid.length > 0) {
                    // Your GCM push server needs to know the regID before it can push to this device
                    // here is where you might want to send it the regID for later use.
                   // alert('step 1 Azure! ' + e.regid);
                    if (mobileServiceClient) {
                       // alert('step 2 Azure!');
                        // Template registration.
                        var template ="{ "data" : {"message":"$(message)"}}";
                        // Register for notifications.
                        mobileServiceClient.push.gcm.registerTemplate(e.regid,
                         "myTemplate", template, null)
                          .done(function () {
                           //   alert('Registered template with Azure!');
                          }).fail(function (error) {
                              //alert('Failed registering with Azure: ' + error);
                          });
                    }



                    console.log("regID =" + e.regid);
                }
                break;

            case 'message':
                // if this flag is set, this notification happened while we were in the foreground.
                // you might want to play a sound to get the user's attention, throw up a dialog, etc.
                if (e.foreground) {

                    // on Android soundname is outside the payload.
                    // On Amazon FireOS all custom attributes are contained within payload
                    var soundfile = e.soundname || e.payload.sound;
                    // if the notification contains a soundname, play it.
                    // playing a sound also requires the org.apache.cordova.media plugin
                    var my_media = new Media("/res/" + beep.wav);
                    alert("Message:"+e.message);
                    my_media.play();
                }
                else {  // otherwise we were launched because the user touched a notification in the notification tray.
                    if (e.coldstart) {

                    }
                    else {

                    }
                }


                break;

            case 'error':
                break;

            default:
                break;
        }
    }


    function tokenHandler(result)
    {
       // $("#app-status-ul").append('
<li>
token: '+ result +'
</li>
');
        // Your iOS push server needs to know the token before it can push to this device
        // here is where you might want to send it the token for later use.
    }

    function successHandler(result) {
        alert("Success handler:" + result);
     //   $("#app-status-ul").append('
<li>
success:'+ result +'
</li>
');
    }

    function errorHandler(error) {
        alert("Error handler:" + error);
    }