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

Commit

Permalink
Merge pull request #16 from Jeepeng/develop
Browse files Browse the repository at this point in the history
iOS SDK 升级到 v3.1.1
  • Loading branch information
Jeepeng authored May 14, 2018
2 parents 73df913 + 891c836 commit a147100
Show file tree
Hide file tree
Showing 14 changed files with 773 additions and 740 deletions.
112 changes: 85 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,48 +48,106 @@ E/NotificationService: No Channel found for pkg=com.jeepeng.push, channelId=null
AppDelegate.m:

```oc
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#import "AppDelegate.h"
#import <XGPush/XGPushManager.h>
#import <XGPush.h>
#import <React/RCTBundleURLProvider.h>
#import <React/RCTRootView.h>
// ...
@implementation AppDelegate
// Required to register for notifications
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[XGPushManager didRegisterUserNotificationSettings:notificationSettings];
NSURL *jsCodeLocation;
jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];
RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
moduleName:@"example"
initialProperties:nil
launchOptions:launchOptions];
rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1];
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
UIViewController *rootViewController = [UIViewController new];
rootViewController.view = rootView;
self.window.rootViewController = rootViewController;
[self.window makeKeyAndVisible];
// 统计消息推送的抵达情况
[[XGPush defaultManager] reportXGNotificationInfo:launchOptions];
return YES;
}
// Required for the register event.
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
[XGPushManager didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSLog(@"[XGDemo] device token is %@", [[XGPushTokenManager defaultTokenManager] deviceTokenString]);
}
// Required for the notification event. You must call the completion handler after handling the remote notification.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
NSLog(@"[XGDemo] register APNS fail.\n[XGDemo] reason : %@", error);
[[NSNotificationCenter defaultCenter] postNotificationName:@"registerDeviceFailed" object:nil];
}
/**
收到通知消息的回调,通常此消息意味着有新数据可以读取(iOS 7.0+)
@param application UIApplication 实例
@param userInfo 推送时指定的参数
@param completionHandler 完成回调
*/
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
NSLog(@"[XGDemo] receive slient Notification");
NSLog(@"[XGDemo] userinfo %@", userInfo);
UIApplicationState state = [application applicationState];
BOOL isClicked = (state != UIApplicationStateActive);
NSMutableDictionary *remoteNotification = [NSMutableDictionary dictionaryWithDictionary:userInfo];
if(isClicked) {
remoteNotification[@"clicked"] = @YES;
remoteNotification[@"background"] = @YES;
}
[XGPushManager didReceiveRemoteNotification:remoteNotification fetchCompletionHandler:completionHandler];
// 统计收到推送的设备
[XGPushManager handleReceiveNotification:remoteNotification successCallback:^{
NSLog(@"[XGPush] Handle receive success");
} errorCallback:^{
NSLog(@"[XGPush] Handle receive error");
}];
[[XGPush defaultManager] reportXGNotificationInfo:remoteNotification];
completionHandler(UIBackgroundFetchResultNewData);
}
// Required for the registrationError event.
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
[XGPushManager didFailToRegisterForRemoteNotificationsWithError:error];
// iOS 10 新增 API
// iOS 10 会走新 API, iOS 10 以前会走到老 API
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
// App 用户点击通知
// App 用户选择通知中的行为
// App 用户在通知中心清除消息
// 无论本地推送还是远程推送都会走这个回调
- (void)xgPushUserNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler {
NSLog(@"[XGDemo] click notification");
if ([response.actionIdentifier isEqualToString:@"xgaction001"]) {
NSLog(@"click from Action1");
} else if ([response.actionIdentifier isEqualToString:@"xgaction002"]) {
NSLog(@"click from Action2");
}
[[XGPush defaultManager] reportXGNotificationResponse:response];
completionHandler();
}
// Required for the localNotification event.
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
[XGPushManager didReceiveLocalNotification:notification];
// App 在前台弹通知需要调用这个接口
- (void)xgPushUserNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
[[XGPush defaultManager] reportXGNotificationInfo:notification.request.content.userInfo];
completionHandler(UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert);
}
#endif
@end
```

## Example
Expand Down
5 changes: 1 addition & 4 deletions example/app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Example extends Component {
} else {
// 请将1111111111修改为APP的AccessId,10位数字
// 请将YOUR_ACCESS_KEY修改为APP的AccessKey
XGPush.init(1111111111, 'YOUR_ACCESS_KEY_IOS');
XGPush.init(2200209997, 'ITD46N87JA4K');
}

XGPush.setHuaweiDebug(true);
Expand All @@ -60,7 +60,6 @@ class Example extends Component {
XGPush.register('jeepeng')
.then(result => {
// do something
// 或者在 onRegister 里处理,效果一样
})
.catch(err => {
console.log(err);
Expand All @@ -86,8 +85,6 @@ class Example extends Component {
*/
_onRegister(deviceToken) {
alert('onRegister: ' + deviceToken);
// 在ios中,register方法是向apns注册,如果要使用信鸽推送,得到deviceToken后还要向信鸽注册
XGPush.registerForXG(deviceToken);
}

/**
Expand Down
Loading

0 comments on commit a147100

Please sign in to comment.