iOS手机差距——推送通知不显示通知服务提示框

Posted

技术标签:

【中文标题】iOS手机差距——推送通知不显示通知服务提示框【英文标题】:iOS phone gap - push notification does not display notification service alert box 【发布时间】:2012-11-01 09:27:07 【问题描述】:

我已尝试通过此处的教程应用 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 函数返回空值? 提前致谢。

【问题讨论】:

您是否向Apple注册以获得推送通知令牌等? 【参考方案1】:

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

【讨论】:

【参考方案2】:

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

【讨论】:

以上是关于iOS手机差距——推送通知不显示通知服务提示框的主要内容,如果未能解决你的问题,请参考以下文章

阿里云推送服务

phonegap ios:当用户为权限请求框选择“不允许”时,推送通知冻结

不显示仅包含数字的 iOS 推送通知

苹果手机怎么设置app消息通知

了解过去是不是在 iOS 上显示过推送通知权限提示

iOS推送通知生产证书通用名称问题