0

我已尝试通过此处的教程应用 iOS 推送通知 -
http://devgirl.org/2012/10/19/tutorial-apple-push-notifications-with-phonegap-part-1/

如教程说明所示,我将推送通知文件夹实现到 xcode 插件文件夹,然后将以下代码添加到 AppDelegate.m 类

#import "PushNotification.h"
    /* START BLOCK */
#pragma PushNotification delegation

- (void)application:(UIApplication*)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
    PushNotification* pushHandler = [self.viewController getCommandInstance:@"PushNotification"];
   [pushHandler didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}

- (void)application:(UIApplication*)appdidFailToRegisterForRemoteNotificationsWithError:  
   (NSError*)error{
      PushNotification* pushHandler = [self.viewController getCommandInstance:@"PushNotification"];
      [pushHandler didFailToRegisterForRemoteNotificationsWithError:error];
}

- (void)application:(UIApplication*)application didReceiveRemoteNotification: (NSDictionary*)userInfo
 {
    PushNotification* pushHandler = [self.viewController   getCommandInstance:@"PushNotification"];
NSMutableDictionary* mutableUserInfo = [userInfo mutableCopy];

// Get application state for iOS4.x+ devices, otherwise assume active
UIApplicationState appState = UIApplicationStateActive;
if ([application respondsToSelector:@selector(applicationState)]) {
    appState = application.applicationState;
}

[mutableUserInfo setValue:@"0" forKey:@"applicationLaunchNotification"];
if (appState == UIApplicationStateActive) {
    [mutableUserInfo setValue:@"1" forKey:@"applicationStateActive"];
    [pushHandler didReceiveRemoteNotification:mutableUserInfo];
} else {
    [mutableUserInfo setValue:@"0" forKey:@"applicationStateActive"];
    [mutableUserInfo setValue:[NSNumber numberWithDouble: [[NSDate date] timeIntervalSince1970]] forKey:@"timestamp"];
    [pushHandler.pendingNotifications addObject:mutableUserInfo];
}
}
/* STOP BLOCK */

并在 javascript 代码下方添加到 index.js

// license to apache software .....
var app = {
    myLog: document.getElementById("log"),
    initialize: function() {
    this.bindEvents();
},
// Bind Event Listeners
bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, false);
document.addEventListener('resume', this.onResume, false);
},
onResume: function() {
    app.myLog.value="";
// Clear the badge number - if a new notification is received it will have a number set on it for the badge
app.setBadge(0);
app.getPending(); // Get pending since we were reopened and may have been launched from a push notification
},
onDeviceReady: function() {
    app.register(); // Call to register device immediately every time since unique token can change (per Apple)

// This will cause to fire when app is active already
document.addEventListener('push-notification', function(event) {
                          console.log('RECEIVED NOTIFICATION! Push-notification! ' + event);
                          app.myLog.value+=JSON.stringify(['\nPush notification received!', event]);
                          // Could pop an alert here if app is open and you still wanted to see your alert
                          //navigator.notification.alert("Received notification - fired Push Event " + JSON.stringify(['push-//notification!', event]));
                          });
document.removeEventListener('deviceready', this.deviceready, false);
},
setBadge: function(num) {
var pushNotification = window.plugins.pushNotification;
app.myLog.value+="Clear badge... \n";
pushNotification.setApplicationIconBadgeNumber(num);
},
receiveStatus: function() {
var pushNotification = window.plugins.pushNotification;
pushNotification.getRemoteNotificationStatus(function(status) {
                                             app.myLog.value+=JSON.stringify(['Registration check - getRemoteNotificationStatus', status])+"\n";
                                             }); 
},
getPending: function() {
var pushNotification = window.plugins.pushNotification;
pushNotification.getPendingNotifications(function(notifications) {
                                         app.myLog.value+=JSON.stringify(['getPendingNotifications', notifications])+"\n";
                                         console.log(JSON.stringify(['getPendingNotifications', notifications]));
                                         });
},
register: function() {
var pushNotification = window.plugins.pushNotification;
pushNotification.registerDevice({alert:true, badge:true, sound:true}, function(status) {
                                app.myLog.value+=JSON.stringify(['registerDevice status: ', status])+"\n";
                                app.storeToken(status.deviceToken);
                                });
},
storeToken: function(token) {
console.log("Token is " + token);
var xmlhttp=new XMLHttpRequest();
xmlhttp.open("POST","http://127.0.0.1:8888",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("token="+token+"&message=pushnotificationtester");
xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState==4) {
        //a response now exists in the responseTest property.
        console.log("Registration response: " + xmlhttp.responseText);
        app.myLog.value+="Registration server returned: " + xmlhttp.responseText;
    }
}
}    
};

似乎 pushNotification.registerDevice 函数返回空状态。而且我仍然不确定为什么通知真实警报(“应用程序”想向您发送推送通知)未显示在 iOS 设备上。我错过了什么吗?是什么让 registerDeivce 函数返回空值?提前致谢。

4

2 回答 2

7

如果注册失败意味着检查应用程序 ID 是否已启用并配置生产或开发推送通知。如果没有启用它并检查或重新配置它并检查它。

于 2013-01-30T09:47:52.990 回答
0

您确定不是 app.myLog 为空吗?也许是因为您没有复制 index.html 并且 html 中没有“日志”元素?

于 2013-01-20T21:44:08.800 回答