Skip to content
This repository was archived by the owner on Apr 11, 2023. It is now read-only.

Commit 9e48480

Browse files
committed
修复 iOS register 监听事件不生效的问题(#17)
1 parent 598f715 commit 9e48480

File tree

2 files changed

+40
-14
lines changed

2 files changed

+40
-14
lines changed

README.md

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,26 @@ AppDelegate.m:
8989
return YES;
9090
}
9191
92+
// Required to register for notifications
93+
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
94+
{
95+
[XGPushManager didRegisterUserNotificationSettings:notificationSettings];
96+
}
97+
9298
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
93-
NSLog(@"[XGDemo] device token is %@", [[XGPushTokenManager defaultTokenManager] deviceTokenString]);
99+
[XGPushManager didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
94100
}
95101
102+
// Required for the registrationError event.
96103
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
97-
NSLog(@"[XGDemo] register APNS fail.\n[XGDemo] reason : %@", error);
98-
[[NSNotificationCenter defaultCenter] postNotificationName:@"registerDeviceFailed" object:nil];
104+
NSLog(@"[XGPush] register APNS fail.\n[XGPush] reason : %@", error);
105+
[XGPushManager didFailToRegisterForRemoteNotificationsWithError:error];
106+
}
107+
108+
// Required for the localNotification event.
109+
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
110+
{
111+
[XGPushManager didReceiveLocalNotification:notification];
99112
}
100113
101114
/**
@@ -106,8 +119,8 @@ AppDelegate.m:
106119
@param completionHandler 完成回调
107120
*/
108121
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
109-
NSLog(@"[XGDemo] receive slient Notification");
110-
NSLog(@"[XGDemo] userinfo %@", userInfo);
122+
NSLog(@"[XGPush] receive slient Notification");
123+
NSLog(@"[XGPush] userinfo %@", userInfo);
111124
UIApplicationState state = [application applicationState];
112125
BOOL isClicked = (state != UIApplicationStateActive);
113126
NSMutableDictionary *remoteNotification = [NSMutableDictionary dictionaryWithDictionary:userInfo];
@@ -116,7 +129,7 @@ AppDelegate.m:
116129
remoteNotification[@"background"] = @YES;
117130
}
118131
[[XGPush defaultManager] reportXGNotificationInfo:remoteNotification];
119-
completionHandler(UIBackgroundFetchResultNewData);
132+
[XGPushManager didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
120133
}
121134
122135
// iOS 10 新增 API
@@ -127,7 +140,7 @@ AppDelegate.m:
127140
// App 用户在通知中心清除消息
128141
// 无论本地推送还是远程推送都会走这个回调
129142
- (void)xgPushUserNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler {
130-
NSLog(@"[XGDemo] click notification");
143+
NSLog(@"[XGPush] click notification");
131144
if ([response.actionIdentifier isEqualToString:@"xgaction001"]) {
132145
NSLog(@"click from Action1");
133146
} else if ([response.actionIdentifier isEqualToString:@"xgaction002"]) {

example/ios/example/AppDelegate.m

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,26 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
3838
return YES;
3939
}
4040

41+
// Required to register for notifications
42+
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
43+
{
44+
[XGPushManager didRegisterUserNotificationSettings:notificationSettings];
45+
}
46+
4147
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
42-
NSLog(@"[XGDemo] device token is %@", [[XGPushTokenManager defaultTokenManager] deviceTokenString]);
48+
[XGPushManager didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
4349
}
4450

51+
// Required for the registrationError event.
4552
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
46-
NSLog(@"[XGDemo] register APNS fail.\n[XGDemo] reason : %@", error);
47-
[[NSNotificationCenter defaultCenter] postNotificationName:@"registerDeviceFailed" object:nil];
53+
NSLog(@"[XGPush] register APNS fail.\n[XGPush] reason : %@", error);
54+
[XGPushManager didFailToRegisterForRemoteNotificationsWithError:error];
55+
}
56+
57+
// Required for the localNotification event.
58+
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
59+
{
60+
[XGPushManager didReceiveLocalNotification:notification];
4861
}
4962

5063
/**
@@ -55,8 +68,8 @@ - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotif
5568
@param completionHandler 完成回调
5669
*/
5770
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
58-
NSLog(@"[XGDemo] receive slient Notification");
59-
NSLog(@"[XGDemo] userinfo %@", userInfo);
71+
NSLog(@"[XGPush] receive slient Notification");
72+
NSLog(@"[XGPush] userinfo %@", userInfo);
6073
UIApplicationState state = [application applicationState];
6174
BOOL isClicked = (state != UIApplicationStateActive);
6275
NSMutableDictionary *remoteNotification = [NSMutableDictionary dictionaryWithDictionary:userInfo];
@@ -65,7 +78,7 @@ - (void)application:(UIApplication *)application didReceiveRemoteNotification:(N
6578
remoteNotification[@"background"] = @YES;
6679
}
6780
[[XGPush defaultManager] reportXGNotificationInfo:remoteNotification];
68-
completionHandler(UIBackgroundFetchResultNewData);
81+
[XGPushManager didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
6982
}
7083

7184
// iOS 10 新增 API
@@ -76,7 +89,7 @@ - (void)application:(UIApplication *)application didReceiveRemoteNotification:(N
7689
// App 用户在通知中心清除消息
7790
// 无论本地推送还是远程推送都会走这个回调
7891
- (void)xgPushUserNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler {
79-
NSLog(@"[XGDemo] click notification");
92+
NSLog(@"[XGPush] click notification");
8093
if ([response.actionIdentifier isEqualToString:@"xgaction001"]) {
8194
NSLog(@"click from Action1");
8295
} else if ([response.actionIdentifier isEqualToString:@"xgaction002"]) {

0 commit comments

Comments
 (0)