ios 開發之 -- 極光推送,發送自定義消息,進入制定頁面


在進行極光推送時候,發現版本有所更新,以前截取didfinish入口方法里面的launchOptions,獲取一個本地的通知內容,進行本地展示不可用了,通過查詢官方文檔和網上的資料才發現,方法改變了,具體方法如下(只針對怎樣定義消息):

1,功能說明

只有在前端運行的時候才能收到自定義消息的推送。

從jpush服務器獲取用戶推送的自定義消息內容和標題以及附加字段等。

2,實現方法

獲取iOS的推送內容需要在delegate類中注冊通知並實現回調方法。

在方法- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *) launchOptions 加入下面的代碼:

 NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
    [defaultCenter addObserver:self selector:@selector(networkDidReceiveMessage:) name:kJPFNetworkDidReceiveMessageNotification object:nil];

實現回調方法:

 - (void)networkDidReceiveMessage:(NSNotification *)notification {
        NSDictionary * userInfo = [notification userInfo];
        NSString *content = [userInfo valueForKey:@"content"];
        NSDictionary *extras = [userInfo valueForKey:@"extras"]; 
        NSString *customizeField1 = [extras valueForKey:@"customizeField1"]; //服務端傳遞的Extras附加字段,key是自己定義的
     
    }

參數描述:

content:獲取推送的內容

extras:獲取用戶自定義參數

customizeField1:根據自定義key獲取自定義的value

更多實現參考 SDK下載壓縮包中的 demo。

3,跳轉目標頁面方法的實現,在第二步的回調方法里面插入如下方法:

[self getPushMessageAtStateActive:userInfo];

方法代碼:

#pragma mark -- 程序運行時收到通知
-(void)getPushMessageAtStateActive:(NSDictionary *)pushMessageDic{
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@""
                                                                             message:[pushMessageDic objectForKey:@"content"]
                                                                      preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"查看"
                                                            style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                                                                
                                                                UIViewController *targetVC = [self topVC:[UIApplication sharedApplication].keyWindow.rootViewController];
                                                                
                                                                [targetVC.navigationController pushViewController:[[AuthenticationViewController alloc] init] animated:YES];
                                                            }];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消"
                                                           style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                                                               
                                                           }];
    
    [alertController addAction:confirmAction];
    [alertController addAction:cancelAction];
    [self.window.rootViewController presentViewController:alertController animated:YES completion:nil];
    
    
}
- (UIViewController *)topVC:(UIViewController *)rootViewController{
    if ([rootViewController isKindOfClass:[UITabBarController class]]) {
        UITabBarController *tab = (UITabBarController *)rootViewController;
        return [self topVC:tab.selectedViewController];
    }else if ([rootViewController isKindOfClass:[UINavigationController class]]){
        UINavigationController *navc = (UINavigationController *)rootViewController;
        return [self topVC:navc.visibleViewController];
    }else if (rootViewController.presentedViewController){
        UIViewController *pre = (UIViewController *)rootViewController.presentedViewController;
        return [self topVC:pre];
    }else{
        return rootViewController;
    }
}

這樣的話,就可以實現,點擊彈出的alerview的確定按鈕,進入指定的頁面了!

 

--------------------------------------------------------------------------------------------------------------

上面說的是發送自定義消息的方法,這里來說下app在后台,或者不活躍狀態時候的操作:

1,通知的三種形式:

typedef NS_OPTIONS(NSUInteger, UNNotificationPresentationOptions) {
    UNNotificationPresentationOptionBadge   = (1 << 0),
    UNNotificationPresentationOptionSound   = (1 << 1),
    UNNotificationPresentationOptionAlert   = (1 << 2),
} 

2,在此方法里面實現:

// iOS 10 Support
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
    // Required
    NSDictionary * userInfo = response.notification.request.content.userInfo;
    if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
        [JPUSHService handleRemoteNotification:userInfo];
    }else is 
{
//本地通知
} completionHandler();
// 系統要求執行這個方法 }

或者:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    
    // Required, iOS 7 Support
    [JPUSHService handleRemoteNotification:userInfo];
    NSLog(@"userinfo is %@",userInfo);
    
    UIViewController *targetVC = [self topVC:[UIApplication sharedApplication].keyWindow.rootViewController];
    targetVC.hidesBottomBarWhenPushed = YES;
    [targetVC.navigationController pushViewController:[[AuthenticationViewController alloc] init] animated:YES];

    completionHandler(UIBackgroundFetchResultNewData);

}

一個是系統的方法,一個是極光的方法,本地化創建一個alertview也是可以實現,也可以直接在系統方法里面,進行操作,比如跳轉到指定頁面之類的都可以的!

 

在目標頁面的話,如果底下的tabbar沒有隱藏的話,可添加如下代碼(僅做參考,隱藏的方法有很多):

-(void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;
}

-(void)viewWillDisappear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
}

 

僅供參考,一些以官方文檔為准!如果不正確的地方,歡迎指正!

附個極光的鏈接:

https://docs.jiguang.cn/jpush/client/iOS/ios_api/#api-ios


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM