好的,當應用程序在前臺運行時,我的推送通知就像魅力一樣工作。但是,當我進入後臺時,應用程序永遠不會收到推送通知。就像通知聽不進去一樣。 所以這是發生了什麼。當應用程序首次啓動時,我可以收到通知。當我關閉並重新打開應用程序時,我可以收到通知。但是當應用程序在後臺關閉時,我無法收到通知。當應用程序進入後臺並且應用程序變爲活動狀態時我會打印出來,所以我知道它不會關閉。因爲它的打印,它將進入後臺,所以它應該運行。 原來這就是我對應用deligate類:IOS應用程序在後臺不顯示推送通知。 Swift
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
//OTHER THINGS
//Open the Push note page
if launchOptions != nil
{
print(launchOptions)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier(myPage)
self.window?.rootViewController = vc
}
//handel push note if app is closed
//Sends it to the regular handler for push notifcaiton
//didrecivepushnotificaiton
if let remoteNotification = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? NSDictionary
{
print("Got A Push Note when I was closed")
self.application(application, didReceiveRemoteNotification: remoteNotification as [NSObject : AnyObject])
}
}
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken
deviceToken: NSData) {
print("DEVICE TOKEN = \(deviceToken)")
//convert the device token into a string
let tokenChars = UnsafePointer<CChar>(deviceToken.bytes)
var token = ""
for i in 0..<deviceToken.length {
token += String(format: "%02.2hhx", arguments: [tokenChars[i]])
}
print("token: " + token)
//store the user device token for apns push notification
loginInformation.setObject(token, forKey: "token")
self.loginInformation.synchronize()
}
// [START ack_message_reception]
func application(application: UIApplication,
didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
print("Recived Push Notificaion")
var myMess :String = ""
var url : String = ""
var myTitle : String = ""
if var alertDict = userInfo["aps"] as? Dictionary<String, String> {
print("Alert Dict")
print(alertDict)
url = alertDict["url"]!
myMess = alertDict["alert"]!
myTitle = alertDict["mytitle"]!
//store the url for the push control view
loginInformation.setObject(url, forKey: "URL")
loginInformation.setObject(myMess, forKey: "Message")
loginInformation.setObject(myTitle, forKey: "Title")
self.loginInformation.synchronize()
}else{print("No go")}
application.applicationIconBadgeNumber = 0
//post notification.
NSNotificationCenter.defaultCenter().postNotificationName("PushReceived", object: nil, userInfo: userInfo)
if myTitle == ""
{
myTitle = 「New Title「
}
if myMess == ""
{
myMess = 「All Hail Gus「
}
let alert = UIAlertController(title: myTitle, message: myMess, preferredStyle: UIAlertControllerStyle.Alert)
//Close Button
alert.addAction(UIAlertAction(title: "次回", style: UIAlertActionStyle.Default, handler: nil))
self.window?.rootViewController?.presentViewController(alert, animated: true, completion: nil)
}
func registrationHandler(registrationToken: String!, error: NSError!) {
}
// [START receive_apns_token_error]
func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError
error: NSError) {
print(error)
}
我想我對這個太所有正確的設置。但我現在不太確定。推送通知確實有效,但我做了很多更改,並且沒有在一段時間內對它們進行測試。
而這正是有效載荷的例子
{"aps":{"alert":"Gus Message.","badge":"1", "url":"https://www.gus.com","mytitle":"Gus Title"}}
你想顯示警告,如果應用程序在後臺並收到推送? – iphonic
我想要小橫幅thingy彈出並告訴他們,這裏有一些有趣的東西:) – MNM
我不知道靜默推送通知是什麼。我剛剛離開https://www.raywenderlich.com/123862/push-notifications-tutorial這個教程,並添加了我需要的信息,以獲取信息 – MNM