【ThirdParty】iOS-友盟推送

Demo:(XSLC項目,未上線)
參考文檔: http://dev.umeng.com/push/ios/integration

開通友盟的消息推送服務
  • 添加應用并上傳證書

因為本項目的開發證書我已經申請過了,當時沒有勾選推送的服務,現在直接選證書進去編輯.創建兩種證書即可,下面演示如何創建開發環境下的證書

文件名為:aps_development.cer的是推送證書


雙擊開發證書,會打開鑰匙串

照樣創建生產的推送證書即可

然后你會發現,推送證書的兩個證書都是可用的了

  • 配置向導
集成推送

注意:友盟推送不支持pod

  • 手動導入SDK


  • 打開推送開關
    點擊項目---->TARGET---->Capabilities,將這里的Push Notification的開關打開

代碼部分
  • 實現基本推送

AppDelegate中

//友盟推送
#import "UMessage.h"
#import <UserNotifications/UserNotifications.h>
實現協議
<UNUserNotificationCenterDelegate>

AppDelegate中的didFinishLaunchingWithOptions中

   //友盟推送
    [UMessage startWithAppkey:@"你的key" launchOptions:launchOptions];
    //注冊通知,如果要使用category的自定義策略,可以參考demo中的代碼。
    [UMessage registerForRemoteNotifications];
    //打開日志,方便調試
    [UMessage setLogEnabled:YES];
    //iOS10必須加下面這段代碼。
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    center.delegate=self;
    UNAuthorizationOptions types10=UNAuthorizationOptionBadge|  UNAuthorizationOptionAlert|UNAuthorizationOptionSound;
    [center requestAuthorizationWithOptions:types10     completionHandler:^(BOOL granted, NSError * _Nullable error)
     {
        if (granted)
        {
            //點擊允許
            //這里可以添加一些自己的邏輯
        }
        else
        {
            //點擊不允許
            //這里可以添加一些自己的邏輯
        }
    }];


此時項目還沒有上線,沒有上線的話需要我們手動一個一個的添加測試設備,需要獲取這個設備的token

//現在是線下環境,用這個來獲取用戶token
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    NSLog(@"%@",[[[[deviceToken description] stringByReplacingOccurrencesOfString: @"<" withString: @""]
                  stringByReplacingOccurrencesOfString: @">" withString: @""]
                 stringByReplacingOccurrencesOfString: @" " withString: @""]);
}

以上代碼,已經實現了基本的推送
此時
APP在前臺顯示時收不到推送
APP在后臺掛起時彈出推送,點擊打開APP,APP外部的未讀數量在進入APP后消失
APP被殺死時彈出推送,點擊打開APP,APP外部的未讀數量在進入APP后消失

  • 實現頁面跳轉,分三種狀態:APP未啟動時,APP在前臺(任何界面),APP在后臺

以下代碼,實現了下面內容
APP未啟動時
在前臺首頁時
在前臺二級頁面時
在后臺時
均可以推送,點擊頂部或下拉屏幕上的推送進入相應的頁面,
APP上均顯示未讀消息數
點擊頂部或下拉屏幕上的推送,或是點擊APP進入應用,再退出應用,APP外的未讀消息數均會消失

但是有以下幾個問題:
1.不管什么時候來推送,頂部或下拉屏幕上都會顯示,同時APP外面有未讀消息數,但是點擊APP直接進入不會跳入相應的頁面
2.當在在前臺首頁時或在前臺二級頁面來推送時,會同時顯示頂部的推送和頁面內的Alert提示框,如果我頂部的推送和Alert提示框均點擊了,則會push兩次推送界面,這個疊加了

代碼如下

### (AppDelegate.h中)
@property (nonatomic,strong) NSDictionary* userInfo;

### (AppDelegate.m中)
import "UMessage.h"
import <UserNotifications/UserNotifications.h>
import "YCFPushController.h"
@interface AppDelegate ()<UNUserNotificationCenterDelegate,UNUserNotificationCenterDelegate,UIAlertViewDelegate>
@end

//pragma mark - 應用程序準備開始運行
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //友盟推送
    [UMessage startWithAppkey:@"591eada875ca35626f000812" launchOptions:launchOptions];
    //注冊通知,如果要使用category的自定義策略,可以參考demo中的代碼。
    [UMessage registerForRemoteNotifications];
    //打開日志,方便調試
    [UMessage setLogEnabled:YES];
    //iOS10必須加下面這段代碼。
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    center.delegate=self;
    UNAuthorizationOptions types10=UNAuthorizationOptionBadge|  UNAuthorizationOptionAlert|UNAuthorizationOptionSound;
    [center requestAuthorizationWithOptions:types10     completionHandler:^(BOOL granted, NSError * _Nullable error)
     {
        if (granted)
        {
            //點擊允許
            //這里可以添加一些自己的邏輯
        }
        else
        {
            //點擊不允許
            //這里可以添加一些自己的邏輯
        }
    }];


    ###1.APP未啟動時添加下面代碼,其實不添加下面這段代碼,APP未啟動時也能收到推送
    NSDictionary* userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
    if(userInfo)
    {
        NSLog(@"未啟動時有消息,保存");
        self.userInfo = userInfo;//[userInfo copy]
    }

}

###2.iOS10以下,APP在前臺(任何界面)和APP在后臺均在下面方法中實現
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    _userInfo = userInfo;
    //關閉友盟自帶的彈出框
    [UMessage setAutoAlert:YES];
    //收到遠程通知
    [UMessage didReceiveRemoteNotification:userInfo];
    if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive)
    {
         NSLog(@"APP內,不知道要干嘛");
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"標題"
                                                            message:@"Test On ApplicationStateActive"
                                                           delegate:self
                                                  cancelButtonTitle:@"不去"
                                                  otherButtonTitles:@"去看看",nil];
        [alertView show];
    }
    else
    {
        NSLog(@"APP外,直接跳轉");
    }
}

###3.iOS10,APP在前臺(任何界面)在下面方法中實現
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler
{
    NSDictionary * userInfo = notification.request.content.userInfo;
    _userInfo = userInfo;
    if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]])
    {
        //自定義彈出框
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"標題"
                                                            message:@"接收到了推送消息"
                                                           delegate:self
                                                  cancelButtonTitle:@"不去"
                                                  otherButtonTitles:@"去看看",nil];
        [alertView show];
        //關閉友盟自帶的彈出框
        [UMessage setAutoAlert:NO];
        [UMessage didReceiveRemoteNotification:userInfo];
    }
    else
    {
        //應用處于前臺時的本地推送接受
    }
    //當應用處于前臺時提示設置,需要哪個可以設置哪一個
    completionHandler(UNNotificationPresentationOptionSound|UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionAlert);
}

###4.iOS10,APP在后臺在下面方法中實現
-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler
{
    NSDictionary * userInfo = response.notification.request.content.userInfo;
    _userInfo = userInfo;
    if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]])
    {
        //應用處于后臺時的遠程推送接受
        [UMessage didReceiveRemoteNotification:userInfo];
    }
    else
    {
        //應用處于后臺時的本地推送接受
    }
    [self didGetPushNotifi];
}

//alertView代理方法
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    //點擊去看看
    if (buttonIndex==1)
    {
       [self didGetPushNotifi];
    }
}

//其他
-(void)didGetPushNotifi
{
    //友盟推送,跳轉到指定頁面,處理程序已啟動(在后臺或者程序內)
    NSString *pushName = [[_userInfo objectForKey:@"aps"] objectForKey:@"url"];
    if(![pushName isNotBlank])
    {
        NSLog(@"無,不操作");
    }
    else
    {
        [self getPushInfo:_userInfo];
    }
}

-(void)getPushInfo:(NSDictionary *)userInfo
{
    NSLog(@"userInfo是什么%@,跳轉至其他界面",userInfo);
    NSString *url = userInfo[@"aps"][@"url"];
    YCFPushController *controller = [[YCFPushController alloc] initWithURL:url];
    [((YCFMainController *)self.window.rootViewController).selectedViewController  pushViewController:controller animated:YES];
}



推送

進入后臺
消息列表的消息是發送給線上用戶的
測試模式的消息是讓未上線的項目使用的,沒有全選中所有安裝項目的設備的入口,只能一個一個的添加

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,441評論 25 708
  • 寫作原因:網上看了很多推送文章都沒有完美的解答我的疑惑;主要有以下兩點,1:推送來了我點擊應用圖標進入應用怎么取到...
    Thebloodelves閱讀 4,567評論 26 71
  • 極光推送: 1.JPush當前版本是1.8.2,其SDK的開發除了正常的功能完善和擴展外也緊隨蘋果官方的步伐,SD...
    Isspace閱讀 6,792評論 10 16
  • 遠方,悠悠地飄來棗花的馥郁清香。我循著這縷醉人的芬芳,如同被無形的絲線牽引,緩緩走進了那片果園。陽光俏皮地透過稀薄...
    廖艷雷閱讀 471評論 0 11
  • 這個周末過得非常非常充實,因為做了好多平時總也沒機會做的小事兒 周五收到幼兒園發來的短信,說周六正常上課...
    燕兒姐陪你闖職場閱讀 178評論 0 0