-
Notifications
You must be signed in to change notification settings - Fork 0
/
CobaltFcmPluginInit.m
142 lines (98 loc) · 4.9 KB
/
CobaltFcmPluginInit.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
//
// CobaltFcmPluginInit.m
// showtime
//
// Created by antoine on 21/06/2017.
// Copyright © 2017 Kristal. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "CobaltFcmPluginInit.h"
#import "FcmManager.h"
#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
@import UserNotifications;
#endif
#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
@interface CobaltFcmPluginInit() <UNUserNotificationCenterDelegate, FIRMessagingDelegate>
@end
#endif
@implementation CobaltFcmPluginInit : NSObject
////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark SINGLETON
////////////////////////////////////////////////////////////////////////////////////////////////
static CobaltFcmPluginInit *sInstance = nil;
+(CobaltFcmPluginInit *)sharedInstance{
@synchronized (self) {
if (sInstance == nil){
sInstance = [[self alloc]init];
}
}
return sInstance;
}
////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark INITIALISATION
////////////////////////////////////////////////////////////////////////////////////////////////
-(id)init{
self = [super init];
return self;
}
////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark FCM CONFIGURATION
////////////////////////////////////////////////////////////////////////////////////////////////
-(void)initFCM:(UIApplication*) application{
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_7_1) {
// iOS 7.1 or earlier
UIRemoteNotificationType allNotificationTypes =
(UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge);
[application registerForRemoteNotificationTypes:allNotificationTypes];
} else {
// iOS 8 or later
// [END_EXCLUDE]
UIUserNotificationType allNotificationTypes =
(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
UIUserNotificationSettings *settings =
[UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
[[UIApplication sharedApplication] registerForRemoteNotifications];
[UNUserNotificationCenter currentNotificationCenter].delegate = self;
}
// [START configure_firebase]
[FIRApp configure];
// [END configure_firebase]
// Add observer for InstanceID token refresh callback.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tokenRefreshNotification:)
name:kFIRInstanceIDTokenRefreshNotification object:nil];
}
- (void)tokenRefreshNotification:(NSNotification *)notification {
// Note that this callback will be fired everytime a new token is generated, including the first
// time. So if you need to retrieve the token as soon as it is available this is where that
// should be done.
NSString *refreshedToken = [[FIRInstanceID instanceID] token];
[FcmManager setToken:refreshedToken];
[FcmManager onTokenReceived];
}
////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark UNUSERNOTIFICATIONCENTER DELEGATE
////////////////////////////////////////////////////////////////////////////////////////////////
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{
completionHandler(UNNotificationPresentationOptionAlert);
}
-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler{
}
////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark FIRMESSAGING DELEGATE
////////////////////////////////////////////////////////////////////////////////////////////////
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
// If you are receiving a notification message while your app is in the background,
// this callback will not be fired till the user taps on the notification launching the application.
// TODO: Handle data of notification
// Uncomment to print message ID.
// NSLog(@"Message ID: %@", userInfo[@"gcm.message_id"]);
// Print full message.
NSLog(@"%@", userInfo);
}
- (void)applicationReceivedRemoteMessage:
(nonnull FIRMessagingRemoteMessage *)remoteMessage{
NSLog(@"%@", remoteMessage);
}
@end