未在通知托盘(顶部)中获取推送通知,而 App 在前台 iOS

人气:454 发布:2022-10-16 标签: ios notifications objective-c unnotificationrequest

问题描述

当应用程序处于前台时,我尝试了很多来获得修改后的通知......通过创建通知服务扩展>

在后台并成功修改但在前台仅在警报正文中获取原始有效负载而不在通知中.

这里在 NotificationService.m 文件

@implementation NotificationService- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {self.contentHandler = contentHandler;self.bestAttemptContent = [request.content mutableCopy];//在这里修改通知内容...NSLog(@"tesrrrr");self.bestAttemptContent.title = [NSString stringWithFormat:@"%@ [修改]", self.bestAttemptContent.title];self.bestAttemptContent.body = [NSString stringWithFormat:@"%@[ 手动添加正文]", self.bestAttemptContent.body];self.contentHandler(self.bestAttemptContent);}{(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {NSDictionary *userInfo1 = userInfo;NSLog(@"userInfo: %@", userInfo1);//self.textView.text = [用户信息描述];//我们可以确定应用程序是否因用户点击操作而启动//按钮或通知是否通过检查传递到已经运行的应用程序//应用状态.如果(application.applicationState == UIApplicationStateActive){//当应用程序在后台时从推送通知中打开/* UILocalNotification *localNotification = [[UILocalNotification alloc] init];localNotification.userInfo = 用户信息;localNotification.soundName = UILocalNotificationDefaultSoundName;localNotification.alertBody = @"xyz";localNotification.fireDate = [NSDate 日期];[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];*/NSLog(@"userInfoUIApplicationStateactive->%@",[userInfo objectForKey:@"aps"]);NSLog(@"userInfoUIApplicationStateactive->%@",[userInfo objectForKey:@"aps"]);UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Did receive a Remote Notification" message:[NSString stringWithFormat:@"您的应用程序名称在运行时收到此通知:\n%@",[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]]delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];[警报视图显示];//[self scheduleAlarmForDate1:[NSDate date]alarmDict:userInfo];}别的{//应用程序运行时的推送通知.以便您可以在任何视图中显示警报和推送NSLog(@"userInfoUIApplicationStateBackground->%@",[userInfo objectForKey:@"aps"]);}}}

解决方案

实施 UNUserNotificationCenterDelegate 委托方法以在应用程序处于前台时获取通知(顶部托盘).但它只适用于 IOS 10.

在您的 didFinishLaunchingWithOptions 方法中,像这样设置 UNUserNotificationCenterDelegate 委托.

[UNUserNotificationCenter currentNotificationCenter].delegate = self;

实现委托方法...

//当通知发送到前台应用时调用.-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{完成处理程序(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge);}//调用以让您的应用程序知道用户为给定通知选择了哪个操作.-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler{完成处理程序();}

注意

如果您的应用程序开发目标小于 IOS10,请使用它来设置委托.

#if 已定义(__IPHONE_10_0) &&__IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0//对于 iOS 10 显示通知(通过 APNS 发送)[UNUserNotificationCenter currentNotificationCenter].delegate = self;#万一

I tried lot to get modified notification while app is foreground .... By Creating Notification Service Extensions

In Background and killed success to modified but in foreground only Getting Original Payload in alert body not in notification .

HERE In NotificationService.m File

@implementation NotificationService
- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request    withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
    self.contentHandler = contentHandler;
    self.bestAttemptContent = [request.content mutableCopy];

    // Modify the notification content here...


    NSLog(@"tesrrrr");

    self.bestAttemptContent.title = [NSString stringWithFormat:@"%@ [modified]", self.bestAttemptContent.title];
    self.bestAttemptContent.body = [NSString stringWithFormat:@"%@[ body  added Manually ]", self.bestAttemptContent.body];



    self.contentHandler(self.bestAttemptContent);
}
{(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    NSDictionary *userInfo1 = userInfo;
    NSLog(@"userInfo: %@", userInfo1);

    //self.textView.text = [userInfo description];
    // We can determine whether an application is launched as a result of the user tapping the action
    // button or whether the notification was delivered to the already-running application by examining
    // the application state.

    if (application.applicationState == UIApplicationStateActive)
    {
        //opened from a push notification when the app was on background


     /*  UILocalNotification *localNotification = [[UILocalNotification alloc] init];
        localNotification.userInfo = userInfo;
        localNotification.soundName = UILocalNotificationDefaultSoundName;
        localNotification.alertBody = @"xyz";
        localNotification.fireDate = [NSDate date];
        [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

        */


        NSLog(@"userInfoUIApplicationStateactive->%@",[userInfo objectForKey:@"aps"]);
        NSLog(@"userInfoUIApplicationStateactive->%@",[userInfo objectForKey:@"aps"]);

        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Did receive a Remote Notification" message:[NSString stringWithFormat:@"Your App name received this notification while it was Running:\n%@",[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]]delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];

        [alertView show];

       // [self scheduleAlarmForDate1:[NSDate date]alarmDict:userInfo];



    }
    else
    {
        // a push notification when the app is running. So that you can display an alert and push in any view

        NSLog(@"userInfoUIApplicationStateBackground->%@",[userInfo objectForKey:@"aps"]);

    }
}}

解决方案

Implement UNUserNotificationCenterDelegate delegate methods to get notification (tray at top) while app is in foreground. But it will only work with IOS 10.

In your didFinishLaunchingWithOptions method set UNUserNotificationCenterDelegate delegate like this.

[UNUserNotificationCenter currentNotificationCenter].delegate = self;

Implement Delegate methods...

//Called when a notification is delivered to a foreground app.
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{

    completionHandler(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge);
}

//Called to let your app know which action was selected by the user for a given notification.
-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler{

    completionHandler();
}

NOTE

If your apps development target is less then IOS10 use this to set the delegate.

#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
    // For iOS 10 display notification (sent via APNS)
    [UNUserNotificationCenter currentNotificationCenter].delegate = self;
#endif

620