/
M2AppDelegate.m
160 lines (130 loc) · 6.25 KB
/
M2AppDelegate.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
//
// M2AppDelegate.m
// m2048
//
// Created by Danqing on 3/16/14.
// Copyright (c) 2014 Danqing. All rights reserved.
//
#import "M2AppDelegate.h"
#import <Analytics/SEGAnalytics.h>
#import <Segment-Amplitude/SEGAmplitudeIntegrationFactory.h>
@implementation M2AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// initialize Segment SDK with Amplitude client-side bundled
SEGAnalyticsConfiguration *config = [SEGAnalyticsConfiguration configurationWithWriteKey:@"hlCKa6PghcMf2DoB9Hxo3RirQ0yYL15r"];
[config use:[SEGAmplitudeIntegrationFactory instance]];
[SEGAnalytics setupWithConfiguration:config];
// use Segment's identify to set a userId with some user properties (traits)
[[SEGAnalytics sharedAnalytics] identify:@"bob@gmail.com" traits:@{@"name": @"Bob"}];
// Add action for remind later.
UIMutableUserNotificationAction *laterAction = [[UIMutableUserNotificationAction alloc] init];
[laterAction setIdentifier:@"later_action_id"];
[laterAction setTitle:@"Later..."];
[laterAction setActivationMode:UIUserNotificationActivationModeBackground];
// Add action for play now.
UIMutableUserNotificationAction *playAction = [[UIMutableUserNotificationAction alloc] init];
[playAction setIdentifier:@"play_action_id"];
[playAction setTitle:@"Play Now!"];
[playAction setActivationMode:UIUserNotificationActivationModeForeground];
UIMutableUserNotificationCategory* category = [[UIMutableUserNotificationCategory alloc] init];
[category setIdentifier:@"reminder_category_id"];
[category setActions:@[laterAction, playAction] forContext:UIUserNotificationActionContextDefault];
NSSet* categories = [NSSet setWithArray:@[category]];
NSUInteger types = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge;
if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
NSLog(@"Requesting permission for push notifications..."); // iOS 8
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:categories];
[UIApplication.sharedApplication registerUserNotificationSettings:settings];
} else {
NSLog(@"Registering device for push notifications..."); // iOS 7 and earlier
[UIApplication.sharedApplication registerForRemoteNotificationTypes:types];
}
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application
{
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[self sendReminderNotification:5];
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
}
- (void)applicationWillTerminate:(UIApplication *)application
{
}
#pragma mark - Push Notifications
- (void)sendReminderNotification:(long)delay
{
// Schedule reminder local notification.
long score = (long)[Settings integerForKey:@"Best Score"];
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.alertBody = [NSString stringWithFormat:@"You got all the way to %ld points. Play again and go higher!", score];
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
localNotification.category = @"reminder_category_id";
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:delay];
localNotification.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
- (void)application:(UIApplication *)application
didRegisterUserNotificationSettings:(UIUserNotificationSettings *)settings
{
NSLog(@"Registering device for push notifications..."); // iOS 8
[application registerForRemoteNotifications];
}
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)token
{
NSLog(@"Registration successful, bundle identifier: %@, device token: %@",
[NSBundle.mainBundle bundleIdentifier], token);
}
- (void)application:(UIApplication *)application
didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
NSLog(@"Failed to register: %@", error);
}
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier
forRemoteNotification:(NSDictionary *)notification completionHandler:(void(^)())completionHandler
{
NSLog(@"Received push notification: %@, identifier: %@", notification, identifier); // iOS 8
// initialize Segment SDK with Amplitude client-side bundled
SEGAnalyticsConfiguration *config = [SEGAnalyticsConfiguration configurationWithWriteKey:@"hlCKa6PghcMf2DoB9Hxo3RirQ0yYL15r"];
[config use:[SEGAmplitudeIntegrationFactory instance]];
[SEGAnalytics setupWithConfiguration:config];
// use Segment's identify to set a userId with some user properties (traits)
[[SEGAnalytics sharedAnalytics] identify:@"bob@gmail.com" traits:@{@"name": @"Bob"}];
completionHandler();
}
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier
forLocalNotification:(UILocalNotification *)notification completionHandler:(void(^)())completionHandler
{
if ([notification.category isEqualToString:@"reminder_category_id"])
{
// initialize Segment SDK with Amplitude client-side bundled
SEGAnalyticsConfiguration *config = [SEGAnalyticsConfiguration configurationWithWriteKey:@"hlCKa6PghcMf2DoB9Hxo3RirQ0yYL15r"];
[config use:[SEGAmplitudeIntegrationFactory instance]];
[SEGAnalytics setupWithConfiguration:config];
// use Segment's identify to set a userId with some user properties (traits)
[[SEGAnalytics sharedAnalytics] identify:@"bob@gmail.com" traits:@{@"name": @"Bob"}];
if ([identifier isEqualToString:@"later_action_id"])
{
[self sendReminderNotification:10];
}
else if ([identifier isEqualToString:@"play_action_id"])
{
NSLog(@"Play now was pressed");
}
}
completionHandler();
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler
{
NSLog(@"Received push notification: %@ %ld", userInfo[@"aps"], (long)application.applicationState); // iOS 7 and earlier
}
@end