当应用程序在前台 iOS 中时,未在通知托盘(顶部)中获取推送通知
Posted
技术标签:
【中文标题】当应用程序在前台 iOS 中时,未在通知托盘(顶部)中获取推送通知【英文标题】:Not Getting push notification in Notification tray (at top) while App in foreground iOS 【发布时间】:2017-06-02 06:35:36 【问题描述】:当应用程序处于前台时,我尝试了很多来获得修改通知.... 通过创建Notification Service Extensions
在后台并杀死成功修改但在前台仅在警报正文中获取原始有效负载而不是在通知中。
这里
在NotificationService.m
文件中
@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"]);
【问题讨论】:
在 ios 10 中你有一个委托方法来处理这种情况,在以前的版本中,你必须在didReceiveRemoteNotification
中处理它
通知完全取决于负载数据及其使用的密钥,您是否启用了project>target>capabilities>Background Modes
下的remote notification
服务(这使应用能够在后台获取通知)。
【参考方案1】:
实现UNUserNotificationCenterDelegate
委托方法以在应用程序处于前台时获取通知(顶部托盘)。但它只适用于 IOS 10。
在你的didFinishLaunchingWithOptions
方法中设置UNUserNotificationCenterDelegate
像这样的委托。
[UNUserNotificationCenter currentNotificationCenter].delegate = self;
实现委托方法...
//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();
注意
如果您的应用程序开发目标小于 IOS10 使用它来设置委托。
#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】:顶部的通知横幅在应用程序处于前台时不显示。此横幅由操作系统管理,仅在应用程序处于后台或终止状态时调用。
虽然 iOS 10 并非如此,但从 iOS 10 开始,您可以这样做, 您需要在委托中捕获通知并调用显示横幅的函数。
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
completionHandler(UNNotificationPresentationOptions.alert)
但正如我所说,这仅适用于 iOS 10+ 。对于以前的兼容性,您可以执行以下操作:
为它创建一个自定义控件。 当应用程序处于前台时,您可以轻松地在委托函数中捕获通知。然后您可以调用您自己的自定义控件(视图)以像横幅一样显示。 幸运的是,有很多很好的通知自定义控件可以做到这一点。 这里有几个:
1.BRYXBanner
2.CRToast
【讨论】:
是的,我正在相应地编辑答案,我认为 OP 想要一个更通用的解决方案以上是关于当应用程序在前台 iOS 中时,未在通知托盘(顶部)中获取推送通知的主要内容,如果未能解决你的问题,请参考以下文章