[英]iOS phone gap - push notification does not display notification service alert box
我已經嘗試過通過此處的教程應用iOS推送通知-
http://devgirl.org/2012/10/19/tutorial-apple-push-notifications-with-phonegap-part-1/
如教程說明所示,我在xcode plugins文件夾中實現了推送通知文件夾,然后在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函數返回空值? 提前致謝。
如果注冊失敗,則意味着檢查應用程序ID是否已啟用並配置了生產或開發推送通知。 如果未啟用,請檢查或重新配置它並檢查它。
您確定不是app.myLog為null嗎? 也許是因為您沒有復制index.html,並且在html中沒有'log'元素?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.