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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
#import "RammusPlugin.h"
NSString *_isSuccessful = @"isSuccessful";
@implementation RammusPlugin {
// iOS 10通知中心
UNUserNotificationCenter *_notificationCenter;
}
+ (void)registerWithRegistrar:(NSObject <FlutterPluginRegistrar> *)registrar {
FlutterMethodChannel *channel = [FlutterMethodChannel
methodChannelWithName:@"com.jarvanmo/rammus"
binaryMessenger:[registrar messenger]];
RammusPlugin *instance = [[RammusPlugin alloc] initWithRegistrar:registrar methodChannel:channel];
[registrar addMethodCallDelegate:instance channel:channel];
[registrar addApplicationDelegate:instance];
}
//__weak NSDictionary *_launchOptions;
FlutterMethodChannel *_methodChannel;
UNNotificationPresentationOptions _notificationPresentationOption = UNNotificationPresentationOptionNone;
- (instancetype)initWithRegistrar:(NSObject <FlutterPluginRegistrar> *)registrar methodChannel:(FlutterMethodChannel *)flutterMethodChannel {
self = [super init];
if (self) {
_methodChannel = flutterMethodChannel;
}
return self;
}
- (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result {
if ([@"initCloudChannel" isEqualToString:call.method]) {
} else if ([@"deviceId" isEqualToString:call.method]) {
result([CloudPushSDK getDeviceId]);
} else if ([@"bindAccount" isEqualToString:call.method]) {
[self bindAccount:call result:result];
} else if ([@"unbindAccount" isEqualToString:call.method]) {
[self unbindAccount:call result:result];
} else if ([@"bindPhoneNumber" isEqualToString:call.method]) {
[self bindPhoneNumber:call result:result];
} else if ([@"unbindPhoneNumber" isEqualToString:call.method]) {
[self unbindPhoneNumber:call result:result];
} else if ([@"bindTag" isEqualToString:call.method]) {
[self bindTag:call result:result];
} else if ([@"unbindTag" isEqualToString:call.method]) {
[self unbindTag:call result:result];
} else if ([@"listTags" isEqualToString:call.method]) {
[self listTags:call result:result];
} else if ([@"addAlias" isEqualToString:call.method]) {
[self addAlias:call result:result];
} else if ([@"removeAlias" isEqualToString:call.method]) {
[self removeAlias:call result:result];
} else if ([@"listAliases" isEqualToString:call.method]) {
[self listAliases:call result:result];
}else if([@"configureNotificationPresentationOption" isEqualToString:call.method]){
[self configureNotificationPresentationOption:call result:result];
}else if([@"setupNotificationManager" isEqualToString:call.method]){
result(@YES);
}else if([@"badgeClean" isEqualToString:call.method]){
[self badgeClean:call result:result];
}else if([@"applicationBadgeNumberClean" isEqualToString:call.method]){
[self applicationBadgeNumberClean:call result:result];
}else {
result(FlutterMethodNotImplemented);
}
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// _launchOptions = launchOptions;
[self registerAPNS:application];
[self initCloudPush];
[self listenerOnChannelOpened];
[self registerMessageReceive];
[CloudPushSDK sendNotificationAck:launchOptions];
return NO;
}
#pragma mark APNs Register
/**
* 向APNs注册,获取deviceToken用于推送
*
* @param application
*/
- (void)registerAPNS:(UIApplication *)application {
float systemVersionNum = [[[UIDevice currentDevice] systemVersion] floatValue];
if (systemVersionNum >= 10.0) {
// iOS 10 notifications
_notificationCenter = [UNUserNotificationCenter currentNotificationCenter];
// 创建category,并注册到通知中心
// [self createCustomNotificationCategory];
_notificationCenter.delegate = self;
// 请求推送权限
[_notificationCenter requestAuthorizationWithOptions:UNAuthorizationOptionAlert | UNAuthorizationOptionBadge | UNAuthorizationOptionSound completionHandler:^(BOOL granted, NSError *_Nullable error) {
if (granted) {
// granted
NSLog(@"User authored notification.");
// 向APNs注册,获取deviceToken
dispatch_async(dispatch_get_main_queue(), ^{
[application registerForRemoteNotifications];
});
} else {
// not granted
NSLog(@"User denied notification.");
}
}];
} else if (systemVersionNum >= 8.0) {
// iOS 8 Notifications
#pragma clang diagnostic push
#pragma clang diagnostic ignored"-Wdeprecated-declarations"
[application registerUserNotificationSettings:
[UIUserNotificationSettings settingsForTypes:
(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge)
categories:nil]];
[application registerForRemoteNotifications];
#pragma clang diagnostic pop
} else {
// iOS < 8 Notifications
#pragma clang diagnostic push
#pragma clang diagnostic ignored"-Wdeprecated-declarations"
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
#pragma clang diagnostic pop
}
}
/**
* 主动获取设备通知是否授权(iOS 10+)
*/
- (void)getNotificationSettingStatus {
[_notificationCenter getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings *_Nonnull settings) {
if (settings.authorizationStatus == UNAuthorizationStatusAuthorized) {
NSLog(@"User authed.");
} else {
NSLog(@"User denied.");
}
}];
}
/*
* APNs注册成功回调,将返回的deviceToken上传到CloudPush服务器
*/
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSLog(@"Upload deviceToken to CloudPush server.");
[CloudPushSDK registerDevice:deviceToken withCallback:^(CloudPushCallbackResult *res) {
if (res.success) {
NSLog(@"Register deviceToken success, deviceToken: %@", [CloudPushSDK getApnsDeviceToken]);
} else {
NSLog(@"Register deviceToken failed, error: %@", res.error);
}
}];
}
/*
* APNs注册失败回调
*/
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
NSLog(@"didFailToRegisterForRemoteNotificationsWithError %@", error);
}
#pragma mark SDK Init
- (void)initCloudPush {
// 正式上线建议关闭
[CloudPushSDK turnOnDebug];
// SDK初始化,无需输入配置信息
// 请从控制台下载AliyunEmasServices-Info.plist配置文件,并正确拖入工程
[CloudPushSDK autoInit:^(CloudPushCallbackResult *res) {
if (res.success) {
NSLog(@"Push SDK init success, deviceId: %@.", [CloudPushSDK getDeviceId]);
} else {
NSLog(@"Push SDK init failed, error: %@", res.error);
}
}];
}
#pragma mark Channel Opened
/**
* 注册推送通道打开监听
*/
- (void)listenerOnChannelOpened {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(onChannelOpened:)
name:@"CCPDidChannelConnectedSuccess"
object:nil];
}
/**
* 推送通道打开回调
*
* @param notification
*/
- (void)onChannelOpened:(NSNotification *)notification {
}
#pragma mark Receive Message
/**
* @brief 注册推送消息到来监听
*/
- (void)registerMessageReceive {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(onMessageReceived:)
name:@"CCPDidReceiveMessageNotification"
object:nil];
}
/**
* 处理到来推送消息
*
* @param notification
*/
- (void)onMessageReceived:(NSNotification *)notification {
CCPSysMessage *message = [notification object];
NSString *title = [[NSString alloc] initWithData:message.title encoding:NSUTF8StringEncoding];
NSString *body = [[NSString alloc] initWithData:message.body encoding:NSUTF8StringEncoding];
// NSLog(@"Receive message title: %@, content: %@.", title, body);
if (![NSThread isMainThread]) {
dispatch_async(dispatch_get_main_queue(), ^{
// if (tempVO.messageContent != nil) {
// [self insertPushMessage:tempVO];
// }
[_methodChannel invokeMethod:@"onMessageArrived" arguments:@{
@"title": title,
@"content": body
}];
});
} else {
[_methodChannel invokeMethod:@"onMessageArrived" arguments:@{
@"title": title,
@"content": body
}];
// if (tempVO.messageContent != nil) {
// [self insertPushMessage:tempVO];
// }
}
}
/**
* 处理iOS 10通知(iOS 10+)
*/
- (void)handleiOS10Notification:(UNNotification *)notification fromFront:(BOOL)fromFront {
UNNotificationRequest *request = notification.request;
UNNotificationContent *content = request.content;
NSDictionary *extras = content.userInfo;
// 通知时间
NSDate *noticeDate = notification.date;
// 标题
NSString *title = content.title;
// 副标题
NSString *subtitle = content.subtitle;
// 内容
NSString *body = content.body;
// 角标
int badge = [content.badge intValue];
// 取得通知自定义字段内容,例:获取key为"Extras"的内容
// NSString *extras = [userInfo valueForKey:@"Extras"];
// 通知角标数清0
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
// 同步角标数到服务端
// [self syncBadgeNum:0];
// 通知打开回执上报
[CloudPushSDK sendNotificationAck:extras];
NSMutableDictionary *result = [[NSMutableDictionary alloc] init];
NSLog(@"Notification, date: %@, title: %@, subtitle: %@, body: %@, badge: %d, extras: %@.", noticeDate, title, subtitle, body, badge, extras);
if (title != nil) {
result[@"title"] = title;
}
if (body != nil) {
result[@"summary"] = body;
}
if (extras != nil) {
result[@"extras"] = [self convertToJsonData:extras];
}
if (subtitle != nil) {
result[@"subtitle"] = subtitle;
}
if (badge != nil) {
result[@"badge"] = @(badge);
}
if (request.identifier != nil) {
result[@"messageId"] = request.identifier;
}
if (fromFront) {
[_methodChannel invokeMethod:@"onNotification" arguments:result];
} else {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[_methodChannel invokeMethod:@"onNotificationOpened" arguments:result];
});
}
}
-(NSString *)convertToJsonData:(NSDictionary *)dict{
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];
NSString *jsonString;
if (!jsonData) {
NSLog(@"%@",error);
}else{
jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
}
NSMutableString *mutStr = [NSMutableString stringWithString:jsonString];
NSRange range = {0,jsonString.length};
//去掉字符串中的空格
[mutStr replaceOccurrencesOfString:@" " withString:@"" options:NSLiteralSearch range:range];
NSRange range2 = {0,mutStr.length};
//去掉字符串中的换行符
[mutStr replaceOccurrencesOfString:@"\n" withString:@"" options:NSLiteralSearch range:range2];
return mutStr;
}
/**
* App处于前台时收到通知(iOS 10+)
*/
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
NSLog(@"Receive a notification in foregound.");
// 处理iOS 10通知,并上报通知打开回执
[self handleiOS10Notification:notification fromFront:YES];
completionHandler(_notificationPresentationOption);
// 通知不弹出
// completionHandler(UNNotificationPresentationOptionNone);
// 通知弹出,且带有声音、内容和角标
// completionHandler(UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionBadge);
}
/**
* 触发通知动作时回调,比如点击、删除通知和点击自定义action(iOS 10+)
*/
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
NSString *userAction = response.actionIdentifier;
// 点击通知打开
if ([userAction isEqualToString:UNNotificationDefaultActionIdentifier]) {
// NSLog(@"User opened the notification.");
// 处理iOS 10通知,并上报通知打开回执
[self handleiOS10Notification:response.notification fromFront:NO];
}
// 通知dismiss,category创建时传入UNNotificationCategoryOptionCustomDismissAction才可以触发
if ([userAction isEqualToString:UNNotificationDismissActionIdentifier]) {
[_methodChannel invokeMethod:@"onNotificationRemoved" arguments:response.notification.request.identifier];
// NSLog(@"User dismissed the notification.");
}
NSString *customAction1 = @"action1";
NSString *customAction2 = @"action2";
// 点击用户自定义Action1
if ([userAction isEqualToString:customAction1]) {
NSLog(@"User custom action1.");
}
// 点击用户自定义Action2
if ([userAction isEqualToString:customAction2]) {
NSLog(@"User custom action2.");
}
completionHandler();
}
- (void)bindAccount:(FlutterMethodCall *)call result:(FlutterResult)result {
[CloudPushSDK bindAccount:(NSString *) call.arguments withCallback:^(CloudPushCallbackResult *res) {
if (res.success) {
if (res.data == nil) {
result(@{_isSuccessful: @YES});
} else {
result(@{_isSuccessful: @YES, @"response": res.data});
};
} else {
result(@{_isSuccessful: @NO, @"errorCode": @(res.error.code), @"errorMessage": res.error.domain, @"iosError": [NSString stringWithFormat:@"%@", res.error]});
}
}];
}
- (void)unbindAccount:(FlutterMethodCall *)call result:(FlutterResult)result {
[CloudPushSDK unbindAccount:^(CloudPushCallbackResult *res) {
if (res.success) {
if (res.data == nil) {
result(@{_isSuccessful: @YES});
} else {
result(@{_isSuccessful: @YES, @"response": res.data});
};
} else {
result(@{_isSuccessful: @NO, @"errorCode": @(res.error.code), @"errorMessage": res.error.domain, @"iosError": [NSString stringWithFormat:@"%@", res.error]});
}
}];
}
- (void)bindPhoneNumber:(FlutterMethodCall *)call result:(FlutterResult)result {
result(@{_isSuccessful: @YES});
// [CloudPushSDK bindAccount:(NSString *) call.arguments withCallback:^(CloudPushCallbackResult *res) {
// if (res.success) {
// if (res.data == nil) {
// result(@{_isSuccessful: @YES});
// } else {
// result(@{_isSuccessful: @YES, @"response": res.data});
//
// };
// } else {
// result(@{_isSuccessful: @NO, @"errorCode": @(res.error.code), @"errorMessage": res.error.domain, @"iosError": [NSString stringWithFormat:@"%@", res.error]});
// }
// }];
}
- (void)unbindPhoneNumber:(FlutterMethodCall *)call result:(FlutterResult)result {
result(@{_isSuccessful: @YES});
// [CloudPushSDK unbindAccount:^(CloudPushCallbackResult *res) {
// if (res.success) {
// if (res.data == nil) {
// result(@{_isSuccessful: @YES});
// } else {
// result(@{_isSuccessful: @YES, @"response": res.data});
//
// };
// } else {
// result(@{_isSuccessful: @NO, @"errorCode": @(res.error.code), @"errorMessage": res.error.domain, @"iosError": [NSString stringWithFormat:@"%@", res.error]});
// }
// }];
}
- (void)bindTag:(FlutterMethodCall *)call result:(FlutterResult)result {
// (title == (id) [NSNull null]) ? nil : title
NSNumber *target = (call.arguments[@"target"] == (id) [NSNull null]) ? @1 : call.arguments[@"target"];
NSString *alias = (call.arguments[@"alias"] == (id) [NSNull null]) ? nil : call.arguments[@"alias"];
[CloudPushSDK bindTag:target.intValue withTags:call.arguments[@"tags"] withAlias:alias withCallback:^(CloudPushCallbackResult *res) {
if (res.success) {
if (res.data == nil) {
result(@{_isSuccessful: @YES});
} else {
result(@{_isSuccessful: @YES, @"response": res.data});
};
} else {
result(@{_isSuccessful: @NO, @"errorCode": @(res.error.code), @"errorMessage": res.error.domain, @"iosError": [NSString stringWithFormat:@"%@", res.error]});
}
}];
}
- (void)unbindTag:(FlutterMethodCall *)call result:(FlutterResult)result {
NSNumber *target = (call.arguments[@"target"] == (id) [NSNull null]) ? @1 : call.arguments[@"target"];
NSString *alias = (call.arguments[@"alias"] == (id) [NSNull null]) ? nil : call.arguments[@"alias"];
[CloudPushSDK unbindTag:target.intValue withTags:call.arguments[@"tags"] withAlias:alias withCallback:^(CloudPushCallbackResult *res) {
if (res.success) {
if (res.data == nil) {
result(@{_isSuccessful: @YES});
} else {
result(@{_isSuccessful: @YES, @"response": res.data});
};
} else {
result(@{_isSuccessful: @NO, @"errorCode": @(res.error.code), @"errorMessage": res.error.domain, @"iosError": [NSString stringWithFormat:@"%@", res.error]});
}
}];
}
- (void)listTags:(FlutterMethodCall *)call result:(FlutterResult)result {
NSNumber *target = (call.arguments == (id) [NSNull null]) ? @1 : call.arguments;
[CloudPushSDK listTags:target.intValue withCallback:^(CloudPushCallbackResult *res) {
if (res.success) {
if (res.data == nil) {
result(@{_isSuccessful: @YES});
} else {
result(@{_isSuccessful: @YES, @"response": res.data});
};
} else {
result(@{_isSuccessful: @NO, @"errorCode": @(res.error.code), @"errorMessage": res.error.domain, @"iosError": [NSString stringWithFormat:@"%@", res.error]});
}
}];
}
- (void)addAlias:(FlutterMethodCall *)call result:(FlutterResult)result {
NSString *alias = (call.arguments == (id) [NSNull null]) ? nil : call.arguments;
[CloudPushSDK addAlias:alias withCallback:^(CloudPushCallbackResult *res) {
if (res.success) {
if (res.data == nil) {
result(@{_isSuccessful: @YES});
} else {
result(@{_isSuccessful: @YES, @"response": res.data});
};
} else {
result(@{_isSuccessful: @NO, @"errorCode": @(res.error.code), @"errorMessage": res.error.domain, @"iosError": [NSString stringWithFormat:@"%@", res.error]});
}
}];
}
- (void)removeAlias:(FlutterMethodCall *)call result:(FlutterResult)result {
NSString *alias = (call.arguments == (id) [NSNull null]) ? nil : call.arguments;
[CloudPushSDK removeAlias:alias withCallback:^(CloudPushCallbackResult *res) {
if (res.success) {
if (res.data == nil) {
result(@{_isSuccessful: @YES});
} else {
result(@{_isSuccessful: @YES, @"response": res.data});
};
} else {
result(@{_isSuccessful: @NO, @"errorCode": @(res.error.code), @"errorMessage": res.error.domain, @"iosError": [NSString stringWithFormat:@"%@", res.error]});
}
}];
}
- (void)listAliases:(FlutterMethodCall *)call result:(FlutterResult)result {
[CloudPushSDK listAliases:^(CloudPushCallbackResult *res) {
if (res.success) {
if (res.data == nil) {
result(@{_isSuccessful: @YES});
} else {
result(@{_isSuccessful: @YES, @"response": res.data});
};
} else {
result(@{_isSuccessful: @NO, @"errorCode": @(res.error.code), @"errorMessage": res.error.domain, @"iosError": [NSString stringWithFormat:@"%@", res.error]});
}
}];
}
- (void)configureNotificationPresentationOption:(FlutterMethodCall *)call result:(FlutterResult)result {
// {"none": none, "sound": sound, "alert": alert, "badge": badge});
BOOL none = [call.arguments[@"none"] boolValue];
if(none){
_notificationPresentationOption = _notificationPresentationOption|UNNotificationPresentationOptionNone;
}
BOOL sound = [call.arguments[@"sound"] boolValue];
if(sound){
_notificationPresentationOption = _notificationPresentationOption |UNNotificationPresentationOptionSound;
}
BOOL alert = [call.arguments[@"alert"] boolValue];
if(alert){
_notificationPresentationOption = _notificationPresentationOption | UNNotificationPresentationOptionAlert;
}
BOOL badge = [call.arguments[@"badge"] boolValue];
if(badge){
_notificationPresentationOption = _notificationPresentationOption | UNNotificationPresentationOptionBadge;
}
result(@YES);
}
- (void)badgeClean:(FlutterMethodCall *)call result:(FlutterResult)result {
int num = [call.arguments[@"num"] intValue];
[CloudPushSDK syncBadgeNum:num withCallback:^(CloudPushCallbackResult *res) {
}];
result(@YES);
}
- (void)applicationBadgeNumberClean:(FlutterMethodCall *)call result:(FlutterResult)result {
int num = [call.arguments[@"num"] intValue];
[UIApplication sharedApplication].applicationIconBadgeNumber = num ;
result(@YES);
}
@end