Interactive push notifications - Hide/Show buttons

2019-09-14 16:28发布

问题:

I followed this tutorial to display buttons on a push notification.
Buttons are registered by calling registerNotification in didFinishLaunchingWithOptions.
However, in few cases I need to display a simple notification without any buttons. How to show/hide the buttons for different notifications ?

回答1:

For adding another kind of interactive notification with no button you will have to update the UIUserNotificationSettings.

Create a new notification category UIMutableUserNotificationCategory without any button:

UIMutableUserNotificationCategory *newNotificationCategory = [[UIMutableUserNotificationCategory alloc] init];
newNotificationCategory.identifier = @"no_button_id";

Then, add this new category to the existing UIUserNotificationSettings:

    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];

Just make sure that the identifier of newNotificationCategory matches with your UILocalNotification's category, in which you dont need any buttons.

Then schedule the notification:

UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:afireDate];
localNotification.alertBody = @"alert body text";
localNotification.category = @"no_button_id"; //  Same as category identifier
localNotification.timeZone = [NSTimeZone systemTimeZone];
localNotification.soundName = SOUND_FILE;
localNotification.repeatInterval = 0;
[[UIApplication sharedApplication]scheduleLocalNotification:localNotification];


回答2:

I hope this is help you.

Parameter Description.
    userInfo --> dictionary value for user needs.
    title    --> notitification Title.
    fireDate --> trigger notification date and time.
    Interactive -> pass flag to set Interactive or normal notification.
-(void)setLocalnotfication:(NSDictionary *)userInfo title:(NSString *)title date:(NSDate *)fireDate Interactive : (BOOL)flag {
   UILocalNotification *notification = [[UILocalNotification alloc] init];
   notification.fireDate = fireDate;
   notification.alertBody = title;
   notification.timeZone = [NSTimeZone defaultTimeZone];
   notification.soundName = UILocalNotificationDefaultSoundName;
   if(flag) notification.category = @"Category name";
   if([userInfo isKindOfClass:[NSDictionary class]]) notification.userInfo = userInfo;
   [[UIApplication sharedApplication] scheduleLocalNotification:notification];

}

i am using this code to create it.

and more swift

func setLocalnotfication(userInfo: [NSObject : AnyObject], title: String, date fireDate: NSDate, Interactive flag: Bool) {
    var notification: UILocalNotification = UILocalNotification()
    notification.fireDate = fireDate
    notification.alertBody = title
    notification.timeZone = NSTimeZone.defaultTimeZone()
    notification.soundName = UILocalNotificationDefaultSoundName
    if flag {
    notification.category = NCI
    }
    if (userInfo is NSDictionary.self) {
    notification.userInfo = userInfo
   }
    UIApplication.sharedApplication().scheduleLocalNotification(notification)
}

Refer the Website