IOS----友盟推送詳解,ios----詳解

來源:互聯網
上載者:User

IOS----友盟推送詳解,ios----詳解

這兩天好好的研究了下推送這功能,關於它我將分成兩部分來講,一、IOS手機端,二、Servlet服務端,今天先講下IOS端

一、感受

下面講下我對推送這個功能在IOS下的感受,這個算是我做了服務端的功能和手機端的功能後的一個體會吧, 這功能在IOS上是多少給我帶來了點雞肋的感覺,首先很多時候收到推送有延時現象,還有如果連續推送兩條訊息給一個Iphone手機,如果遇到延時這個問題,那麼第一條推送就會被覆蓋。(這裡我用的是友盟的推送,這個推送暫時感覺還不錯,大多數推送能及時收到,至少我暫時測試是如此,所以我這裡也選擇了它,以前遇到過兩小時後才到的第三方推送.....甚至根本就收不到。),再一個,即使延遲這個問題解決了,關於推送訊息的管理也是一個麻煩事,如果想好好管理,我覺得資料庫裡面首先得建立一張對應的通知管理表,今天在IOS上面我是認真的測試了這功能,當應用從後台移除後,接受到的推送訊息,是不會進入通知代理的,這一點是個麻煩事,(也就是這一點,讓我想到了資料庫中建立對應的表。),應為使用者不可能一直把手機拿手上,即使拿手上,就看那通知在手機上的短暫停留時間,也可能導致使用者沒看明白後台推送的什麼,而且這時候應用處於被移出背景狀態,由於這個原因,接受到的通知就不能進入通知代理,所以導致這條通知無法儲存到本地。所以說建立一張表我覺得是必要的。 但我發現很多應用都沒這樣做。 如果這樣做了,對推送來說是一個很大的最佳化,至少在IOS上是,因為IOS暫時無法完全避免接受不到通知這個揪心的問題,而且我覺得發送的訊息內容都不重要了。感覺可以把通知的語音修改一下,這一點才重要.....改成這樣:你有新訊息請在通知表中去查看....  

 

二、下載友盟推送SDK整合到應用

    這裡註冊我就不說了,下面我主要以的方式講解。

   1、下面的點擊"SDK下載及文檔"

     

   2、上一步執行後,進入新頁面,找到訊息推送。

    

    3、上一步執行後,進入新頁面,點擊SDK下載。

 

     

  4、將下載的SDK推送包COPY到項目中。

  

  

三、匯出推送認證上傳友盟,建立應用

    1、友盟的推送只需要匯出P12認證即可,不需要產生pem認證,這裡需要匯出兩個認證,一個是開發人員認證,一個是產品認證,下面。

           

      你如果還不知道怎麼製作Ios推送認證,可以去 http://www.cnblogs.com/xiaoliao/p/4928873.html 查看,講得非常詳細。

    2、下面是友盟建立IOS應用的步驟

         首先進入帳號管理後台,可以從首頁右上方點擊 我的產品進入。

          

         進入後點擊頁面左下角的 +增加新應用

        

     進入建立應用介面,建立應用,這裡由於我是示範給各位看,所以隨便填的。

    

上面步驟執行完畢後,拖動瀏覽器捲軸就會看到下面的

下面的我要說下,當你執行完上面的步驟之後,你就給選擇的應用建立了推送的功能,然後你在看這個應用的就是下面的

的樣式,下面這個是可以隨意修改的,即使你上一步傳錯了認證,或則其他動作有誤,建立完畢後也可以修改。下面是,這裡

要說一個重點:就是伺服器IP的增加,如果這裡不增加伺服器IP,WEB服務端是無法將通知推送到你手機的。這裡的IP就是Servlt架設到伺服器的IP,我這裡由於

是在本機上做的servlet介面推送,所以就是我原生IP了,如果架設到伺服器之後,這個IP就是要修改成伺服器的IP。

 

 

四、IOS上實現推送功能的代碼

   1、只貼通知部分代碼,Appdelegate.m裡面授權。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {   //asdf;lajksdfl;asdfl;             /*友盟推送*/    [UMessage startWithAppkey:@"568f2ccbe0f55a3e05?" launchOptions:launchOptions];    #if __IPHONE_OS_VERSION_MAX_ALLOWED >= _IPHONE80_    if(UMSYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0"))    {        UIMutableUserNotificationAction *action1 = [[UIMutableUserNotificationAction alloc] init];        action1.identifier = @"action1_identifier";        action1.title=@"Accept";        action1.activationMode = UIUserNotificationActivationModeForeground;//當點擊的時候啟動程式                UIMutableUserNotificationAction *action2 = [[UIMutableUserNotificationAction alloc] init];  //第二按鈕        action2.identifier = @"action2_identifier";        action2.title=@"Reject";        action2.activationMode = UIUserNotificationActivationModeBackground;//當點擊的時候不啟動程式,在幕後處理        action2.authenticationRequired = YES;//需要解鎖才能處理,如果action.activationMode = UIUserNotificationActivationModeForeground;則這個屬性被忽略;        action2.destructive = YES;                UIMutableUserNotificationCategory *categorys = [[UIMutableUserNotificationCategory alloc] init];        categorys.identifier = @"category1";//這組動作的唯一標示        [categorys setActions:@[action1,action2] forContext:(UIUserNotificationActionContextDefault)];                UIUserNotificationSettings *userSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert                                                                                     categories:[NSSet setWithObject:categorys]];        [UMessage registerRemoteNotificationAndUserNotificationSettings:userSettings];            } else{        //register remoteNotification types (iOS 8.0以下)        [UMessage registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge         |UIRemoteNotificationTypeSound         |UIRemoteNotificationTypeAlert];    }#else        //register remoteNotification types (iOS 8.0以下)    [UMessage registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge     |UIRemoteNotificationTypeSound     |UIRemoteNotificationTypeAlert];    #endif    //for log    [UMessage setLogEnabled:YES];    }

    2、註冊裝置Token

 - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{    //下面這個token是將擷取的nsdata轉換成String,應為指定推送時我們需要將這個傳給服務端。    NSString *token=[NSString stringWithFormat:@"%@",[[[[deviceToken description] stringByReplacingOccurrencesOfString: @"<" withString: @""]                  stringByReplacingOccurrencesOfString: @">" withString: @""]                 stringByReplacingOccurrencesOfString: @" " withString: @""]];    //註冊token    [UMessage registerDeviceToken:deviceToken];}

     3、接收推送

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{        //接受服務端推播通知傳來的值,全部在userinfo裡面。    [UMessage didReceiveRemoteNotification:userInfo];    [UIApplication sharedApplication].applicationIconBadgeNumber=5;        if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)    {        UIUserNotificationType myType = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;        UIUserNotificationSettings *mySetting = [UIUserNotificationSettings settingsForTypes:myType categories:nil];        [[UIApplication sharedApplication] registerUserNotificationSettings:mySetting];            }else{                UIRemoteNotificationType myType = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:myType];    }      }

  四、管理後台測試,手動發送推送。()

    

   下面的要說一下,4的一步,增加測試裝置,由於我這裡使用的是開發人員模式,所以這裡我們需要將

測試是被的TOKEN增加到進去,擷取TOKEN的方法就是上一個步驟的( 2、註冊裝置Token,的第一句代碼)

  

 

 

 

 明天會增加服務端servlet的實現。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.