Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
- {
- // I know we still receive the notification `userInfo` payload in the foreground.
- // This question is about displaying the stock iOS notification alert UI.
- // Yes, one *could* use a 3rd party toast alert framework.
- [self use3rdPartyToastAlertFrameworkFromGithub]
- }
- optional func userNotificationCenter(_ center: UNUserNotificationCenter,
- willPresent notification: UNNotification,
- withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void)
- - (void)userNotificationCenter:(UNUserNotificationCenter *)center
- willPresentNotification:(UNNotification *)notification
- withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler;
- func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
- completionHandler(UNNotificationPresentationOptions.alert)
- }
- - (void)application:(UIApplication *)applicationdidReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
- NSString *notifMessage = [[userInfo objectForKey:@"aps"] objectForKey:@"alert"];
- //Define notifView as UIView in the header file
- [_notifView removeFromSuperview]; //If already existing
- _notifView = [[UIView alloc] initWithFrame:CGRectMake(0, -70, self.window.frame.size.width, 80)];
- [_notifView setBackgroundColor:[UIColor blackColor]];
- UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10,15,30,30)];
- imageView.image = [UIImage imageNamed:@"AppLogo.png"];
- UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 15, self.window.frame.size.width - 100 , 30)];
- myLabel.font = [UIFont fontWithName:@"Helvetica" size:10.0];
- myLabel.text = notifMessage;
- [myLabel setTextColor:[UIColor whiteColor]];
- [myLabel setNumberOfLines:0];
- [_notifView setAlpha:0.95];
- //The Icon
- [_notifView addSubview:imageView];
- //The Text
- [_notifView addSubview:myLabel];
- //The View
- [self.window addSubview:_notifView];
- UITapGestureRecognizer *tapToDismissNotif = [[UITapGestureRecognizer alloc] initWithTarget:self
- action:@selector(dismissNotifFromScreen)];
- tapToDismissNotif.numberOfTapsRequired = 1;
- tapToDismissNotif.numberOfTouchesRequired = 1;
- [_notifView addGestureRecognizer:tapToDismissNotif];
- [UIView animateWithDuration:1.0 delay:.1 usingSpringWithDamping:0.5 initialSpringVelocity:0.1 options:UIViewAnimationOptionCurveEaseIn animations:^{
- [_notifView setFrame:CGRectMake(0, 0, self.window.frame.size.width, 60)];
- } completion:^(BOOL finished) {
- }];
- //Remove from top view after 5 seconds
- [self performSelector:@selector(dismissNotifFromScreen) withObject:nil afterDelay:5.0];
- return;
- }
- //If the user touches the view or to remove from view after 5 seconds
- - (void)dismissNotifFromScreen{
- [UIView animateWithDuration:1.0 delay:.1 usingSpringWithDamping:0.5 initialSpringVelocity:0.1 options:UIViewAnimationOptionCurveEaseIn animations:^{
- [_notifView setFrame:CGRectMake(0, -70, self.window.frame.size.width, 60)];
- } completion:^(BOOL finished) {
- }];
- }
- application:didReceiveLocalNotification:
- application:didReceiveRemoteNotification:
- -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
- {
- // Use a 3rd party toast alert framework to display a banner
- [self toastAlertFromGithub]
- }
- @available(iOS 10.0, *)
- func userNotificationCenter(center: UNUserNotificationCenter, willPresentNotification notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void)
- {
- completionHandler([UNNotificationPresentationOptions.Alert,UNNotificationPresentationOptions.Sound,UNNotificationPresentationOptions.Badge])
- }
- {
- "aps":{
- "alert":"Testing.. (7)",
- "badge":1,"sound":"default"
- },
- "content-available":1
- }
- UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
- content.body = body;
- content.userInfo = userInfo;
- content.sound = [UNNotificationSound defaultSound];
- [content setValue:@(YES) forKeyPath:@"shouldAlwaysAlertWhileAppIsForeground"];
- UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"Notif" content:content trigger:nil];
- [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
- DLog(@"Error:%@", error);
- }];
- if (application.applicationState == UIApplicationStateActive ) {
- UILocalNotification *localNotification = [[UILocalNotification alloc] init];
- localNotification.userInfo = userInfo;
- localNotification.soundName = UILocalNotificationDefaultSoundName;
- localNotification.alertBody = message;
- localNotification.fireDate = [NSDate date];
- [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
- }
- if #available(iOS 10.0, *)
- {
- // need to setup the global notification delegate somewhere when your app starts
- //
- UNUserNotificationCenter.current().delegate = applicationDelegate
- // to show a message
- //
- let content = UNMutableNotificationContent()
- content.body = "MESSAGE"
- let request = UNNotificationRequest(identifier: "fred", content: content, trigger: nil)
- UNUserNotificationCenter.current().add(request)
- {
- error in // called when message has been sent
- debugPrint("Error: (error)")
- }
- }
- @available(iOS 10.0, *)
- public func userNotificationCenter(_ center : UNUserNotificationCenter, willPresent notification : UNNotification, withCompletionHandler completionHandler : @escaping (UNNotificationPresentationOptions) -> Void)
- {
- completionHandler([.alert]) // only-always show the alert
- }
- let banner = Banner(title: "title", subtitle: "subtitle", image: UIImage(named: "addContact"), backgroundColor: UIColor(red:137.0/255.0, green:172.0/255.0, blue:2.0/255.0, alpha:1.000))
- banner.dismissesOnTap = true
- banner.show(duration: 1.0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement