在大量搜索和跟踪苹果的文档之后,我仍然无法在远程(按下)通知中获得动作按钮,但我通过遵循相同的本地通知代码在本地通知中获得了操作按钮。
- (void)triggerAndRegisterNotification {
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.0")) {
// create actions
#if XCODE_VERSION_GREATER_THAN_OR_EQUAL_TO_8
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
// create actions
UNNotificationAction *acceptAction = [UNNotificationAction actionWithIdentifier:@"com.AG.yes"
title:@"Save"
options:UNNotificationActionOptionForeground];
UNNotificationAction *declineAction = [UNNotificationAction actionWithIdentifier:@"com.AG.no"
title:@"Decline"
options:UNNotificationActionOptionDestructive];
UNNotificationAction *snoozeAction = [UNNotificationAction actionWithIdentifier:@"com.AG.snooze"
title:@"Snooze"
options:UNNotificationActionOptionDestructive];
NSArray *notificationActions = @[ acceptAction, declineAction, snoozeAction ];
// create a category
UNNotificationCategory *inviteCategory = [UNNotificationCategory categoryWithIdentifier:CYLInviteCategoryIdentifier actions:notificationActions intentIdentifiers:@[] options:UNNotificationCategoryOptionNone];
NSSet *categories = [NSSet setWithObject:inviteCategory];
// registration
[center setNotificationCategories:categories];
#endif
} else if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")) {
// create actions
UIMutableUserNotificationAction *acceptAction =
[[UIMutableUserNotificationAction alloc] init];
acceptAction.identifier = @"com.AG.yes";
acceptAction.title = @"Accept";
acceptAction.activationMode =
UIUserNotificationActivationModeBackground;
acceptAction.destructive = NO;
acceptAction.authenticationRequired = NO; //If YES requies
passcode, but does not unlock the device
UIMutableUserNotificationAction *declineAction =
[[UIMutableUserNotificationAction alloc] init];
declineAction.identifier = @"com.AG.no";
acceptAction.title = @"Decline";
acceptAction.activationMode =
UIUserNotificationActivationModeBackground;
declineAction.destructive = YES;
acceptAction.authenticationRequired = NO;
UIMutableUserNotificationAction *snoozeAction =
[[UIMutableUserNotificationAction alloc] init];
snoozeAction.identifier = @"com.AG.snooze";
acceptAction.title = @"Snooze";
snoozeAction.activationMode =
UIUserNotificationActivationModeBackground;
snoozeAction.destructive = YES;
snoozeAction.authenticationRequired = NO;
// create a category
UIMutableUserNotificationCategory *inviteCategory =
[[UIMutableUserNotificationCategory alloc] init];
inviteCategory.identifier = CYLInviteCategoryIdentifier;
NSArray *notificationActions = @[ acceptAction, declineAction,
snoozeAction ];
[inviteCategory setActions:notificationActions
forContext:UIUserNotificationActionContextDefault];
[inviteCategory setActions:notificationActions
forContext:UIUserNotificationActionContextMinimal];
// registration
NSSet *categories = [NSSet setWithObject:inviteCategory];
UIUserNotificationType types = UIUserNotificationTypeBadge |
UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *settings =
[UIUserNotificationSettings settingsForTypes:types
categories:categories];
[[UIApplication sharedApplication]
registerUserNotificationSettings:settings];
}
/// 2. request authorization for localNotification
[self registerNotificationSettingsCompletionHandler:^(BOOL
granted,
NSError * _Nullable error) {
if (granted) {
NSLog(@"request authorization succeeded!");
[[UIApplication sharedApplication]
registerForRemoteNotifications];
}
}];
**COMMENTED CODE FOR LOCAL NOTIFICATION**
// if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.0")) {
// #if XCODE_VERSION_GREATER_THAN_OR_EQUAL_TO_8
// Deliver the notification at 08:30 everyday
// NSDateComponents *dateComponents = [[NSDateComponents
alloc] init];
// dateComponents.hour = 8;
// dateComponents.minute = 30;
// UNCalendarNotificationTrigger *trigger =
// [UNCalendarNotificationTrigger
triggerWithDateMatchingComponents:dateComponents
repeats:YES];
// UNMutableNotificationContent *content =
[[UNMutableNotificationContent alloc] init];
// content.title = [NSString
localizedUserNotificationStringForKey:@"AG said:"
arguments:nil];
// content.body = [NSString
localizedUserNotificationStringForKey:@"Hello Tom!Get up,
let's play with Jerry!" arguments:nil];
// content.sound = [UNNotificationSound
defaultSound];
// content.categoryIdentifier =
CYLInviteCategoryIdentifier;
/// 4. update application icon badge number
// content.badge = @([[UIApplication sharedApplication]
applicationIconBadgeNumber] + 1);
// content.launchImageName = @"any string is ok,such as
微博@iOS程序犭袁";
// Deliver the notification in five seconds.
//*** Terminating app due to uncaught exception
'NSInternalInconsistencyException', reason: 'time interval
must be at least 60 if repeating'
// UNTimeIntervalNotificationTrigger *trigger =
[UNTimeIntervalNotificationTrigger
// triggerWithTimeInterval:60.0f repeats:YES];
// UNNotificationRequest *request = [
UNNotificationRequest
requestWithIdentifier:@"FiveSecond"
// content:content trigger:trigger];
// UNUserNotificationCenter *center =
[UNUserNotificationCenter currentNotificationCenter];
/// 3. schedule localNotification,The delegate must be set before
the application returns from
applicationDidFinishLaunching:.
// center.delegate = self;
// [center addNotificationRequest:request
withCompletionHandler:^(NSError * _Nullable error) {
// if (!error) {
// NSLog(@"add NotificationRequest succeeded!");
// }
// }];
//#endif
// } else {
/// 3. schedule localNotification
// UILocalNotification *localNotification =
[[UILocalNotification alloc] init];
// localNotification.fireDate = [NSDate
dateWithTimeIntervalSinceNow:5.f];
// localNotification.alertTitle = @"AG said:";
// localNotification.alertBody = @"Hello Tom!Get up,
let's play with Jerry!";
// localNotification.alertAction = @"play with Jerry";
//Identifies the image used as the launch image when the user
taps (or slides) the action button (or slider).
// localNotification.alertLaunchImage =
@"LaunchImage.png";
// localNotification.userInfo = @{ @"CategoryIdentifier"
: CYLInviteCategoryIdentifier };
//
// localNotification.timeZone = [NSTimeZone
defaultTimeZone];
//repeat evey minute, 0 means don't repeat
// localNotification.repeatInterval = NSCalendarUnitMinute;
/// 4. update application icon badge number
// localNotification.applicationIconBadgeNumber =
[[UIApplication sharedApplication]
applicationIconBadgeNumber] + 1;
// [[UIApplication sharedApplication]
scheduleLocalNotification:localNotification];
//
// }
}
/// 3. THIS IS THE METHOD TO AUTHORIZE THE NOTIFICATION
- (void)registerNotificationSettingsCompletionHandler:(void (^)(BOOL
granted, NSError *__nullable error))completionHandler; {
/// 2. request authorization for localNotification
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.0")) {
#if XCODE_VERSION_GREATER_THAN_OR_EQUAL_TO_8
UNUserNotificationCenter *center = [UNUserNotificationCenter
currentNotificationCenter];
[center requestAuthorizationWithOptions:
(UNAuthorizationOptionBadge | UNAuthorizationOptionSound
| UNAuthorizationOptionAlert)
completionHandler:completionHandler];
#endif
} else if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0"))
{
// UIUserNotificationSettings
*userNotificationSettings = [UIUserNotificationSettings
settingsForTypes:(UIUserNotificationTypeAlert |
UIUserNotificationTypeSound |
UIUserNotificationTypeBadge)
//
categories:nil];
// UIApplication *application = [UIApplication
sharedApplication];
// [application
registerUserNotificationSettings:userNotificationSettings];
//FIXME:
// !completionHandler ?: completionHandler(granted, error);
}
}
**AND IN** APPDELEGATE.m
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.0")) {
#if XCODE_VERSION_GREATER_THAN_OR_EQUAL_TO_8
/// schedule localNotification, the delegate must be set before
// the application returns fromapplicationDidFinishLaunching:.
UNUserNotificationCenter *center = [UNUserNotificationCenter
currentNotificationCenter];
center.delegate = self;
#endif
}
[self triggerAndRegisterNotification];
}我正在使用iphone 7进行测试。请帮我解决这个问题。提前谢谢
有效载荷JSon
aps = {
alert = {
"artist_id" = 16912;
body = "Kurt Rosenwinkel is playing at Joe Henderson Lab at
SFJAZZ Center";
eventid = 149687805;
sound = default;
timestamp = "810b6035-e4d7-4722-81db-7455e81a48fe";
title = "Kurt Rosenwinkel";
tracks = "itunes,spotify";
type = 2;
};
category = "com.wcities.notification";
};我检查了我在应用程序中设置的类别标识符,它与负载json中的类别标识符相同。
更新
在调试过程中,从上面的代码中,我得到了带有操作按钮的推送通知,但是在某些地方,在did finish launching之后,我将我的windows根视图控制器更改为查看控制器,这是IIViewDeckController的一个子控件。在注释完这一行后,推送通知将带有操作按钮。我完全不明白为什么会发生这种情况,因为据我所知,如果我设置、呈现或推送任何视图控制器,就不应该对推送通知产生任何影响。
如果我做错了什么,请告诉我。我已经分享了上面所有的代码和场景。谢谢
发布于 2017-05-30 07:19:23
由于我使用的是dockview库,所以在码头视图控制器中添加的视图控制器有一些与setting categories as nil.That相关的代码,这是我无法在远程通知中获得操作按钮的原因,所以我删除了这段额外的代码,一切正常。我还在找那个写这段代码的愚蠢的人。接受帮助
发布于 2017-05-23 08:37:11
在ios 10中,通知与-(void)userNotificationCenter:(UNUserNotificationCenter *)中心willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{ //代码UNNotification}一起工作。
-(void)userNotificationCenter:(UNUserNotificationCenter *)中心didReceiveNotificationResponse:(UNNotificationResponse *)响应withCompletionHandler:(void(^)())completionHandler{ }
https://stackoverflow.com/questions/43937678
复制相似问题