Advertisement
Guest User

Untitled

a guest
Oct 28th, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.07 KB | None | 0 0
  1. /**
  2. * Modified MIT License
  3. *
  4. * Copyright 2016 OneSignal
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * 1. The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * 2. All copies of substantial portions of the Software may only be used in connection
  17. * with services provided by OneSignal.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. */
  27.  
  28. #import "AppDelegate.h"
  29. #import <OneSignal/OneSignal.h>
  30.  
  31. @implementation AppDelegate
  32.  
  33. - (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
  34.  
  35. UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  36.  
  37. // TODO: Test before;
  38. center.delegate = self;
  39.  
  40. // Eanble logging to help debug issues. visualLevel will show alert dialog boxes.
  41. [OneSignal setLogLevel:ONE_S_LL_VERBOSE visualLevel:ONE_S_LL_INFO];
  42.  
  43. [OneSignal initWithLaunchOptions:launchOptions appId:@"b2f7f966-d8cc-11e4-bed1-df8f05be55ba" handleNotificationReceived:^(OSNotification *notification) {
  44. NSLog(@"Received Notification - %@", notification.payload.notificationID);
  45. } handleNotificationAction:^(OSNotificationOpenedResult *result) {
  46.  
  47. // This block gets called when the user reacts to a notification received
  48. OSNotificationPayload* payload = result.notification.payload;
  49.  
  50. NSString* messageTitle = @"OneSignal Example";
  51. NSString* fullMessage = [payload.body copy];
  52.  
  53. if (payload.additionalData) {
  54.  
  55. if(payload.title)
  56. messageTitle = payload.title;
  57.  
  58. if (result.action.actionID)
  59. fullMessage = [fullMessage stringByAppendingString:[NSString stringWithFormat:@"\nPressed ButtonId:%@", result.action.actionID]];
  60. }
  61.  
  62. UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:messageTitle
  63. message:fullMessage
  64. delegate:self
  65. cancelButtonTitle:@"Close"
  66. otherButtonTitles:nil, nil];
  67. [alertView show];
  68.  
  69. } settings:@{kOSSettingsKeyInFocusDisplayOption : @(OSNotificationDisplayTypeNotification), kOSSettingsKeyAutoPrompt : @NO}];
  70.  
  71. [OneSignal IdsAvailable:^(NSString *userId, NSString *pushToken) {
  72. if(pushToken) {
  73. NSLog(@"Received push token - %@", pushToken);
  74. NSLog(@"User ID - %@", userId);
  75. }
  76. }];
  77.  
  78. // TODO: Test After;
  79. // center.delegate = self;
  80.  
  81. /*
  82. // iOS 10 ONLY - Add category for the OSContentExtension
  83. // Make sure to add UserNotifications framework in the Linked Frameworks & Libraries.
  84.  
  85. [[UNUserNotificationCenter currentNotificationCenter] getNotificationCategoriesWithCompletionHandler:^(NSSet<UNNotificationCategory *> * _Nonnull categories) {
  86.  
  87. UNNotificationAction* myAction = [UNNotificationAction actionWithIdentifier:@"action0" title:@"Hit Me!" options:UNNotificationActionOptionForeground];
  88. UNNotificationCategory* myCategory = [UNNotificationCategory categoryWithIdentifier:@"myOSContentCategory" actions:@[myAction] intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction];
  89. NSSet* mySet = [[NSSet alloc] initWithArray:@[myCategory]];
  90.  
  91. //Add existing cateogories
  92. mySet = [mySet setByAddingObjectsFromSet:categories];
  93.  
  94. [[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:mySet];
  95. }];
  96. */
  97.  
  98. return YES;
  99. }
  100.  
  101. - (void)userNotificationCenter:(UNUserNotificationCenter *)center
  102. willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{
  103. NSLog( @"for handling push in foreground" );
  104. NSLog(@"%@", notification.request.content.userInfo);
  105. }
  106.  
  107. - (void)userNotificationCenter:(UNUserNotificationCenter *)center
  108. didReceiveNotificationResponse:(UNNotificationResponse *)response
  109. withCompletionHandler:(void (^)(void))completionHandler {
  110.  
  111. NSLog(@"HERE!!!!!");
  112. completionHandler();
  113. }
  114.  
  115. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement