Advertisement
Guest User

Untitled

a guest
Jul 26th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.34 KB | None | 0 0
  1. -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
  2. {
  3. // I know we still receive the notification `userInfo` payload in the foreground.
  4. // This question is about displaying the stock iOS notification alert UI.
  5.  
  6. // Yes, one *could* use a 3rd party toast alert framework.
  7. [self use3rdPartyToastAlertFrameworkFromGithub]
  8. }
  9.  
  10. optional func userNotificationCenter(_ center: UNUserNotificationCenter,
  11. willPresent notification: UNNotification,
  12. withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void)
  13.  
  14. - (void)userNotificationCenter:(UNUserNotificationCenter *)center
  15. willPresentNotification:(UNNotification *)notification
  16. withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler;
  17.  
  18. func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
  19. completionHandler(UNNotificationPresentationOptions.alert)
  20. }
  21.  
  22. - (void)application:(UIApplication *)applicationdidReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
  23.  
  24.  
  25. NSString *notifMessage = [[userInfo objectForKey:@"aps"] objectForKey:@"alert"];
  26.  
  27. //Define notifView as UIView in the header file
  28. [_notifView removeFromSuperview]; //If already existing
  29.  
  30. _notifView = [[UIView alloc] initWithFrame:CGRectMake(0, -70, self.window.frame.size.width, 80)];
  31. [_notifView setBackgroundColor:[UIColor blackColor]];
  32.  
  33. UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10,15,30,30)];
  34. imageView.image = [UIImage imageNamed:@"AppLogo.png"];
  35.  
  36. UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 15, self.window.frame.size.width - 100 , 30)];
  37. myLabel.font = [UIFont fontWithName:@"Helvetica" size:10.0];
  38. myLabel.text = notifMessage;
  39.  
  40. [myLabel setTextColor:[UIColor whiteColor]];
  41. [myLabel setNumberOfLines:0];
  42.  
  43. [_notifView setAlpha:0.95];
  44.  
  45. //The Icon
  46. [_notifView addSubview:imageView];
  47.  
  48. //The Text
  49. [_notifView addSubview:myLabel];
  50.  
  51. //The View
  52. [self.window addSubview:_notifView];
  53.  
  54. UITapGestureRecognizer *tapToDismissNotif = [[UITapGestureRecognizer alloc] initWithTarget:self
  55. action:@selector(dismissNotifFromScreen)];
  56. tapToDismissNotif.numberOfTapsRequired = 1;
  57. tapToDismissNotif.numberOfTouchesRequired = 1;
  58.  
  59. [_notifView addGestureRecognizer:tapToDismissNotif];
  60.  
  61.  
  62. [UIView animateWithDuration:1.0 delay:.1 usingSpringWithDamping:0.5 initialSpringVelocity:0.1 options:UIViewAnimationOptionCurveEaseIn animations:^{
  63.  
  64. [_notifView setFrame:CGRectMake(0, 0, self.window.frame.size.width, 60)];
  65.  
  66. } completion:^(BOOL finished) {
  67.  
  68.  
  69. }];
  70.  
  71.  
  72. //Remove from top view after 5 seconds
  73. [self performSelector:@selector(dismissNotifFromScreen) withObject:nil afterDelay:5.0];
  74.  
  75.  
  76. return;
  77.  
  78.  
  79. }
  80.  
  81. //If the user touches the view or to remove from view after 5 seconds
  82. - (void)dismissNotifFromScreen{
  83.  
  84. [UIView animateWithDuration:1.0 delay:.1 usingSpringWithDamping:0.5 initialSpringVelocity:0.1 options:UIViewAnimationOptionCurveEaseIn animations:^{
  85.  
  86. [_notifView setFrame:CGRectMake(0, -70, self.window.frame.size.width, 60)];
  87.  
  88. } completion:^(BOOL finished) {
  89.  
  90.  
  91. }];
  92.  
  93.  
  94. }
  95.  
  96. application:didReceiveLocalNotification:
  97. application:didReceiveRemoteNotification:
  98.  
  99. -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
  100. {
  101. // Use a 3rd party toast alert framework to display a banner
  102. [self toastAlertFromGithub]
  103. }
  104.  
  105. @available(iOS 10.0, *)
  106. func userNotificationCenter(center: UNUserNotificationCenter, willPresentNotification notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void)
  107. {
  108. completionHandler([UNNotificationPresentationOptions.Alert,UNNotificationPresentationOptions.Sound,UNNotificationPresentationOptions.Badge])
  109. }
  110.  
  111. {
  112. "aps":{
  113. "alert":"Testing.. (7)",
  114. "badge":1,"sound":"default"
  115. },
  116. "content-available":1
  117. }
  118.  
  119. UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
  120. content.body = body;
  121. content.userInfo = userInfo;
  122. content.sound = [UNNotificationSound defaultSound];
  123. [content setValue:@(YES) forKeyPath:@"shouldAlwaysAlertWhileAppIsForeground"];
  124.  
  125. UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"Notif" content:content trigger:nil];
  126. [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
  127. DLog(@"Error:%@", error);
  128. }];
  129.  
  130. if (application.applicationState == UIApplicationStateActive ) {
  131. UILocalNotification *localNotification = [[UILocalNotification alloc] init];
  132. localNotification.userInfo = userInfo;
  133. localNotification.soundName = UILocalNotificationDefaultSoundName;
  134. localNotification.alertBody = message;
  135. localNotification.fireDate = [NSDate date];
  136. [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
  137. }
  138.  
  139. if #available(iOS 10.0, *)
  140. {
  141. // need to setup the global notification delegate somewhere when your app starts
  142. //
  143. UNUserNotificationCenter.current().delegate = applicationDelegate
  144.  
  145. // to show a message
  146. //
  147. let content = UNMutableNotificationContent()
  148. content.body = "MESSAGE"
  149.  
  150. let request = UNNotificationRequest(identifier: "fred", content: content, trigger: nil)
  151. UNUserNotificationCenter.current().add(request)
  152. {
  153. error in // called when message has been sent
  154.  
  155. debugPrint("Error: (error)")
  156. }
  157. }
  158.  
  159. @available(iOS 10.0, *)
  160. public func userNotificationCenter(_ center : UNUserNotificationCenter, willPresent notification : UNNotification, withCompletionHandler completionHandler : @escaping (UNNotificationPresentationOptions) -> Void)
  161. {
  162. completionHandler([.alert]) // only-always show the alert
  163. }
  164.  
  165. 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))
  166. banner.dismissesOnTap = true
  167. banner.show(duration: 1.0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement