Not Getting push notification in Notification tray (at top) while App in foreground iOS

拟墨画扇 提交于 2020-01-05 05:39:21

问题


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

    }
}}

回答1:


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



回答2:


The notification banner at the top, isn't shown while the app is in foreground. This banner is managed by the OS, and is only invoked when the app is in background or in a terminated state.

Although this is NOT the case for iOS 10, as of iOS 10 you can do this, You need to catch the notification in the delegate and invoke the function for showing the banner.

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

But as I said this is only for iOS 10+ . For previous compatibility you can do what is mentioned below :

Create a custom control for it. You can easily catch the notification in the delegate function when the app is in foreground. and then you can invoke your own custom control (a view) to show up like a banner. Luckily there are many good notification custom control to do that. Here are a few :

1.BRYXBanner

2.CRToast



来源:https://stackoverflow.com/questions/44322319/not-getting-push-notification-in-notification-tray-at-top-while-app-in-foregro

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!