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関数がnullステータスを返すようです。そして、通知の本物のアラート(「アプリケーション」がプッシュ通知を送信したい)がiOSデバイスに表示されない理由はまだわかりません。私は何かが足りないのですか?registerDeivce関数がnull値を返す理由は何ですか?前もって感謝します。

4

2 に答える 2

7

登録が失敗した場合は、アプリIDが有効になっていて、本番または開発のプッシュ通知が構成されていることを確認してください。そうでない場合は、有効にして確認または再構成して確認してください。

于 2013-01-30T09:47:52.990 に答える
0

nullであるのはapp.myLogではありませんか?おそらく、index.htmlをコピーしておらず、htmlに「log」要素がないためですか?

于 2013-01-20T21:44:08.800 に答える