2

我想在应用程序处于前台时显示通知。下面是我为新的 usernotificationdelegate 方法所做的代码。

在应用程序委托中:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.0")) {

        //iOS 10 handling
        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
        center.delegate = self;
        [center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) {
          if (!error) {
                 NSLog(@"request authorization succeeded!");
          } }];
     } 
}

#pragma mark - User notification delegate
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler {

    NSLog(@"willPresentNotification");
    NSLog(@"%@", notification.request.content.userInfo);
    completionHandler (UNNotificationPresentationOptionAlert);
}

这是我触发本地通知的方法

-(void) fireLocalNotification:(NSString *) message
{

    NSLog(@"fire Local Notification");
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.0")) {

        //Notification Content
        UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
        content.body =  [NSString stringWithFormat:@"%@",message];
        content.sound = [UNNotificationSound defaultSound];

        //Set Badge Number
        content.badge = @([[UIApplication sharedApplication] applicationIconBadgeNumber] + 1);

        // Deliver the notification in five seconds.
        UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger
                                                      triggerWithTimeInterval:1.0f repeats:NO];

        //Notification Request
        UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"LocalNotification" content:content trigger:trigger];

        //schedule localNotification
        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
        center.delegate = self;
        [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
            if (!error) {
                NSLog(@"add Notification Request succeeded!");
            }
        }];

    }
 }

现在在这样做之后我仍然没有在前台收到通知。

提前谢谢

4

5 回答 5

11

请实现这个功能:

-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
{
    completionHandler(UNNotificationPresentationOptionAlert + UNNotificationPresentationOptionSound);
}
于 2016-10-17T04:08:22.323 回答
1

为了在应用程序处于前台时获得通知,您需要在 iOS10 中实现 UNUserNotificationCenterDelegate 的以下委托方法。

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

    completionHandler([.alert,.badge])
}

有关更多信息,您可以参考苹果文档。链接在这里:

https://developer.apple.com/documentation/usernotifications/unusernotificationcenterdelegate?language=objc

于 2017-06-15T11:16:24.800 回答
0

要在应用程序处于前台时显示横幅消息,请使用以下方法。

将其添加到 AppDelegate 中的委托方法并符合此协议 - UNUserNotificationCenterDelegate

iOS 10、斯威夫特 3:

@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    completionHandler(UNNotificationPresentationOptions.alert)
}
于 2017-07-07T16:25:22.710 回答
0

请参阅此代码以在前台处理本地通知。/*******************************/

  UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];

            UNAuthorizationOptions options = UNAuthorizationOptionAlert + UNAuthorizationOptionSound;

            [center requestAuthorizationWithOptions:options
                                  completionHandler:^(BOOL granted, NSError * _Nullable error) {
                                      if (!granted) {
                                          //NSLog(@"Something went wrong");
                                      }
                                  }];

            int dayCounter =5;

            int minute = 48;


           {
                NSDateComponents *components = [[NSDateComponents alloc] init];
                components.weekday = dayCounter;


                 dayCounter++;

                components.hour = 12;
                components.minute = minute;

                UNCalendarNotificationTrigger *trigger =  [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:YES];

                UNMutableNotificationContent *objNotificationContent = [[UNMutableNotificationContent alloc] init];
                objNotificationContent.title = [NSString localizedUserNotificationStringForKey:@"Notification!" arguments:nil];

                objNotificationContent.body = [NSString localizedUserNotificationStringForKey:@"We made a surprise Edit for You" arguments:nil];

                objNotificationContent.sound = [UNNotificationSound defaultSound];

                objNotificationContent.badge = @([[UIApplication sharedApplication] applicationIconBadgeNumber] + 1);


                UNNotificationAttachment *attachment = nil;

                NSURL* outputURL = [[NSURL alloc] initFileURLWithPath:filePath];
                NSError *attachmentError = nil;

                attachment = [UNNotificationAttachment attachmentWithIdentifier:@"image"
                                                                            URL: outputURL
                                                                        options:nil
                                                                          error:&attachmentError];
                if (attachmentError) {

                    return;
                }

                objNotificationContent.attachments=@[attachment];

                NSString *identifier = @"UYLLocalNotification";
                UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier
                                                                                      content:objNotificationContent trigger: trigger];

                [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
                    if (error != nil) {
                        NSLog(@"Something went wrong: %@",error);
                    }
                    else
                    {
                    }
                }];

/****************************/

// 处理 -(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{

    UIApplicationState state = [application applicationState];
    if (state == UIApplicationStateActive) {

        dispatch_async(dispatch_get_main_queue(), ^{
            UIAlertView *alert =[[UIAlertView alloc]initWithTitle:@"Reminder" message:notification.alertBody delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Yes", nil];
            alert.tag = 330;
            [alert show];
        });
    }
else{
 // handle for background
}
于 2017-03-22T10:48:59.747 回答
0

在屏幕顶部显示警报

-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler
{
    completionHandler(UNNotificationPresentationOptionAlert + UNNotificationPresentationOptionSound);

}
于 2017-03-22T10:40:06.723 回答