2014-09-30 104 views
3

我目前正在開發由8製作的交互式iOS通知,說實話,我真的很難看到我的按鈕。 用下面的代碼,我看不到我的按鈕:iOS自定義按鈕不顯示爲交互式通知

// INTERACIVE

UIMutableUserNotificationAction *acceptAction = 
[[UIMutableUserNotificationAction alloc] init]; 

acceptAction.identifier = @"ACCEPT_IDENTIFIER"; 
acceptAction.title = @"Accept"; 
acceptAction.activationMode = UIUserNotificationActivationModeBackground; 
acceptAction.destructive = NO; 
acceptAction.authenticationRequired = NO; 

UIMutableUserNotificationAction *maybeAction = 
[[UIMutableUserNotificationAction alloc] init]; 

maybeAction.identifier = @"MAYBE_IDENTIFIER"; 
maybeAction.title = @"Maybe"; 
maybeAction.activationMode = UIUserNotificationActivationModeBackground; 
maybeAction.destructive = NO; 
maybeAction.authenticationRequired = YES; 


UIMutableUserNotificationAction *declineAction = 
[[UIMutableUserNotificationAction alloc] init]; 

declineAction.identifier = @"DECLINE_IDENTIFIER"; 
declineAction.title = @"Decline"; 
declineAction.activationMode = UIUserNotificationActivationModeBackground; 
declineAction.destructive = YES; 
declineAction.authenticationRequired = NO; 


UIMutableUserNotificationCategory *inviteCategory = 
[[UIMutableUserNotificationCategory alloc] init]; 

inviteCategory.identifier = @"INVITE_CATEGORY"; 

[inviteCategory setActions:@[acceptAction, maybeAction, declineAction] 
       forContext:UIUserNotificationActionContextDefault]; 

[inviteCategory setActions:@[acceptAction, declineAction] 
       forContext:UIUserNotificationActionContextMinimal]; 


NSSet *categories = [NSSet setWithObjects:inviteCategory, nil]; 
UIUserNotificationType types = UIUserNotificationTypeBadge | 
UIUserNotificationTypeSound | UIUserNotificationTypeAlert; 
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:categories]; 
[[UIApplication sharedApplication] registerUserNotificationSettings:settings]; 
[[UIApplication sharedApplication] registerForRemoteNotifications]; 

UILocalNotification* localNotification = [[UILocalNotification alloc] init]; 
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:10]; 
localNotification.alertBody = @"Testing"; 
localNotification.category = @"INVITE_CATEGORY"; // Same as category identifier 
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; 

我的問題任何想法? 儘管在互聯網上的所有例子,我顯然是唯一一個這個問題。

回答

8

有一個真正偉大的教程在這裏:在你的代碼The Code Ninja

一切都看起來不錯。我必須要求確保您不只是忽略實際發生的事情 - 如果您在主屏幕上查看通知,您是否在通知上向左滑動以顯示按鈕?

這裏是我使用(抱歉關於SWIFT)

初始提示的通知

var openAction = UIMutableUserNotificationAction() 
    openAction.identifier = "OPEN_IDENTIFIER" 
    openAction.title = "Open" 
    openAction.destructive = false 
    openAction.authenticationRequired = false 
    openAction.activationMode = .Foreground 

    var snoozeAction = UIMutableUserNotificationAction() 
    snoozeAction.identifier = "SNOOZE_IDENTIFIER" 
    snoozeAction.title = "Snooze" 
    snoozeAction.destructive = true 
    snoozeAction.authenticationRequired = false 
    snoozeAction.activationMode = .Background 

    var snoozeable = UIMutableUserNotificationCategory() 
    snoozeable.identifier = NOTE_SNOOZE_CAT 
    snoozeable.setActions([openAction, snoozeAction], forContext: .Default) 
    snoozeable.setActions([openAction, snoozeAction], forContext: .Default) 

    var notSnoozable = UIMutableUserNotificationCategory() 
    notSnoozable.identifier = NOTE_NO_SNOOZE_CAT 
    notSnoozable.setActions([openAction], forContext: .Default) 
    notSnoozable.setActions([openAction], forContext: .Default) 

    UIApplication.sharedApplication().registerUserNotificationSettings(UIUserNotificationSettings(forTypes: .Sound | .Alert | 
     .Badge, categories: NSSet(array:[snoozeable, notSnoozable]) 
     )) 

本地通知

var notification    = UILocalNotification() 
    notification.fireDate  = fireDate 
    notification.timeZone  = NSTimeZone.defaultTimeZone() 
    notification.alertBody  = "Alert Body" 
    notification.userInfo  = userInfo 
    notification.category  = blnCanSnooze ? NOTE_SNOOZE_CAT : NOTE_NO_SNOOZE_CAT 
    UIApplication.sharedApplication().scheduleLocalNotification(notification) 
+1

您的教程有效!謝謝你,兄弟! – NiClou 2014-10-23 13:17:46

+0

我想在第二個'snoozeable.setActions([openAction,snoozeAction],forContext:.Default)',你打算用'.Minimal'替換'.Default'。 'notSnoozable'也一樣。 – MichaelScaria 2015-06-05 18:21:04

+0

@Stephen:您是否知道提醒應用程序是如何決定是使用「最小」上下文還是使用「默認」上下文的操作類別? – 2015-10-11 04:12:50

3

我有一個不同的問題是什麼。我正在處理多種類型的通知。 我的通知中幾乎沒有顯示動作按鈕,而很少有人是。發現只有最後設置的通知才顯示動作按鈕。我很愚蠢,因爲設置是共享的,所以不要認爲類別的標識符被上次設置的通知覆蓋。 爲了處理這個問題,我們需要提取當前的通知設置,我們添加到它,並重新設置回:

NSMutableArray *arrNewCategories = [NSMutableArray new]; 
    UIUserNotificationSettings *oldSettings = [[UIApplication sharedApplication]currentUserNotificationSettings]; 
    for (UIMutableUserNotificationCategory *oldCategory in oldSettings.categories) 
    { 
     if (![oldCategory.identifier isEqualToString:newNotificationCategory.identifier]) 
      [arrNewCategories addObject:oldCategory]; 
    } 
    [arrNewCategories addObject:newNotificationCategory]; 

    UIUserNotificationType notificationType = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert; 
    UIUserNotificationSettings *newSettings = [UIUserNotificationSettings settingsForTypes:notificationType categories:[NSSet setWithArray:arrNewCategories]]; 

    [[UIApplication sharedApplication] registerUserNotificationSettings:newSettings]; 

只要確保newNotificationCategory的標識符與您UILocalNotification的類別相匹配。

+0

我得到了與我的本地通知相同的問題,我應該把這個代碼和什麼是oldCategory,newNotificationCategory? – 2016-05-18 13:11:22

+0

無論你在哪裏創建通知,你都應該遵循這一點。爲了添加每個通知,您還需要添加一個UIMutableUserNotificationCategory,在這裏爲我的新通知它是newNotificationCategory與新的設置,循環中的oldCategory對象將包含所有您已經添加的通知的類別。 – Zaraki 2016-05-19 02:09:03

+0

看起來不錯,謝謝! – 2016-05-19 05:24:28