无法在 iOS8 上设置交互式推送通知

Posted

技术标签:

【中文标题】无法在 iOS8 上设置交互式推送通知【英文标题】:Not able to set Interactive Push Notifications on iOS8 【发布时间】:2014-07-18 03:38:26 【问题描述】:

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

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

//
//  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 上,我将 Push 负载设置如下:


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

我收到了推送,但没有使用我设置的自定义按钮。

【问题讨论】:

我不清楚什么不适合你。 didReceiveRemoteNotification 是否被调用? 是的。推送正在工作并出现,但交互式按钮不起作用。我认为与类别有关 @Nick : 你有没有找到任何解决方案,我们可以在收到通知时显示交互式按钮? @RayofHope 查看本教程 youtube.com/watch?v=Yh3lLpV1k_Y 我按照它进行操作。而且您只需要在远程通知有效负载中添加“类别”即可使远程通知也显示交互式按钮(请查看下面的答案) 【参考方案1】:

我不确定我的问题是否与您的相同(确保您的问题不是 Parse 造成的)。只需在此处发布我的解决方案,以防其他人遇到同样的问题。

我的问题在于通知类别。

确保注册通知设置时已经设置好类别(我用的是Objective-C,差别不大):

UIMutableUserNotificationCategory *notificationCategory = [[UIMutableUserNotificationCategory alloc] init];
            notificationCategory.identifier = @"CallNotificationCategory";
            [notificationCategory setActions:@[declineAction, answerAction] forContext:UIUserNotificationActionContextDefault];

            NSSet *categories = [[NSSet alloc] initWithObjects:notificationCategory, nil];

并且,当您发送远程通知时,请确保您在有效负载中具有“类别”,并且该值与您在客户端中定义的值相同。就我而言,它类似于:


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

【讨论】:

【参考方案2】:

这是以防万一有人在使用 Firebase 远程通知时遇到此问题。

请后台开发者发送:

"notification" : 
  "title" : YOUR_TITLE,
  "body" : YOUR_BODY,
  "click_action" : YOUR_CATEGORY_NAME

【讨论】:

【参考方案3】:

您需要在注册 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()

【讨论】:

【参考方案4】:

这对我来说很有效,我可以通过 Parse 在 Swift 中显示和使用交互式推送通知。请注意,您需要为要创建的每个交互按钮创建一个UIMutableNotificationAction。下面的源代码更详细地介绍了按钮的配置选项。

在您的应用委托文件中:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool 

    let notificationTypes:UIUserNotificationType = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound

    var notificationActionAccept :UIMutableUserNotificationAction = UIMutableUserNotificationAction()
    notificationActionAccept.identifier = "ACCEPT_IDENTIFIER"
    notificationActionAccept.title = "Accept"
    notificationActionAccept.destructive = true
    notificationActionAccept.authenticationRequired = false
    notificationActionAccept.activationMode = UIUserNotificationActivationMode.Background

    var notificationCategory:UIMutableUserNotificationCategory = UIMutableUserNotificationCategory()
    notificationCategory.identifier = "CallNotificationCategory"
    notificationCategory .setActions([notificationActionAccept], forContext: UIUserNotificationActionContext.Default)

    let notificationSettings:UIUserNotificationSettings = UIUserNotificationSettings(forTypes: notificationTypes, categories: NSSet(array:[notificationCategory]))
    UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettings)

    return true

在 Parse Cloud 中,当您发送推送时,您会将 category 匹配到 UIMutableUserNotificationCategory,就像上面提到的 @Xialin 一样。

看起来您没有设置类别,或者至少在您注册 UINotificationSettings 之后才设置它们 - 您需要先设置它们,否则它将不起作用。

我在下面的链接中获得了大部分信息。如果需要,它会更详细地介绍。希望这会有所帮助:

http://thecodeninja.tumblr.com/post/90742435155/notifications-in-ios-8-part-2-using-swift-what-is

【讨论】:

以上是关于无法在 iOS8 上设置交互式推送通知的主要内容,如果未能解决你的问题,请参考以下文章

本地通知和推送通知

解析服务器无法发送推送通知?

如何在 android 和 ios 收到的推送通知上设置徽章?

使用交互式推送通知 iOS 8 调用 Web 服务

Web 应用推送通知,无法在浏览器上推送通知

MobileFirst - Android 推送 - 无法订阅,通知令牌未在服务器上更新