无法在 iOS8 上设置交互式推送通知 [英] Not able to set Interactive Push Notifications on iOS8

查看:17
本文介绍了无法在 iOS8 上设置交互式推送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经能够设置交互式本地通知,但远程通知不起作用.我正在使用 Parse.com 发送 JSON

I was already able to set Interactive LOCAL notifications, but the Remote notifications aren't working. I'm using Parse.com to send the JSON

我的 AppDelegate.Swift 看起来像这样:

My AppDelegate.Swift looks like this:

//
//  AppDelegate.swift
//  SwifferApp
//
//  Created by Training on 29/06/14.
//  Copyright (c) 2014 Training. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
        UINavigationBar.appearance().barTintColor = UIColor.orangeColor()
        UINavigationBar.appearance().tintColor = UIColor.whiteColor()


        Parse.setApplicationId("eUEC7O4Jad0Kt3orqRouU0OJhkGuE20n4uSfrLYE", clientKey: "WypmaQ8XyqH26AeWIANttqwUjRJR4CIM55ioXvez")

        let notificationTypes:UIUserNotificationType = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound
        let notificationSettings:UIUserNotificationSettings = UIUserNotificationSettings(forTypes: notificationTypes, categories: nil)

        UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettings)

        return true
    }

    func application(application: UIApplication!, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings!) {
        UIApplication.sharedApplication().registerForRemoteNotifications()
    }

    func application(application: UIApplication!, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData!) {
        let currentInstallation:PFInstallation = PFInstallation.currentInstallation()
        currentInstallation.setDeviceTokenFromData(deviceToken)
        currentInstallation.saveInBackground()
    }

    func application(application: UIApplication!, didFailToRegisterForRemoteNotificationsWithError error: NSError!) {
        println(error.localizedDescription)
    }

    func application(application: UIApplication!, didReceiveRemoteNotification userInfo:NSDictionary!) {

        var notification:NSDictionary = userInfo.objectForKey("aps") as NSDictionary

        if notification.objectForKey("content-available"){
            if notification.objectForKey("content-available").isEqualToNumber(1){
                NSNotificationCenter.defaultCenter().postNotificationName("reloadTimeline", object: nil)
            }
        }else{
            PFPush.handlePush(userInfo)
        }
    }

    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }
}

在 Parse 上,我像这样设置推送负载:

on Parse, I'm setting the Push payload like this:

{
"alert": "Tune in for the World Series, tonight at 8pm EDT",
"badge": "Increment",
"sound": "chime",
"category": "FIRST_CATEGORY"
}

并且我收到推送,但没有使用我设置的自定义按钮.

and I receive the push, but not with the custom buttons I've set.

推荐答案

注册APNS时需要通过categories.

You need to pass categories while registering for APNS.

看看我的样本:

   var replyAction : UIMutableUserNotificationAction = UIMutableUserNotificationAction()
    replyAction.identifier = "REPLY_ACTION"
    replyAction.title = "Yes, I need!"

    replyAction.activationMode = UIUserNotificationActivationMode.Background
    replyAction.authenticationRequired = false

    var replyCategory : UIMutableUserNotificationCategory = UIMutableUserNotificationCategory()
    replyCategory.identifier = "REPLY_CATEGORY"

    let replyActions:NSArray = [replyAction]

    replyCategory.setActions(replyActions, forContext: UIUserNotificationActionContext.Default)
    replyCategory.setActions(replyActions, forContext: UIUserNotificationActionContext.Minimal)

    let categories = NSSet(object: replyCategory)



    let settings : UIUserNotificationType = UIUserNotificationType.Sound | UIUserNotificationType.Alert | UIUserNotificationType.Badge
    UIApplication.sharedApplication().registerUserNotificationSettings(UIUserNotificationSettings(forTypes: settings, categories: categories))
    UIApplication.sharedApplication().registerForRemoteNotifications()

这篇关于无法在 iOS8 上设置交互式推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆