ios8以上遠程推送 demo

來源:互聯網
上載者:User

標籤:ios 推送 通知 遠程推送 push


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    // Override point for customization after application launch.

    

    //註冊推播通知

//    [[UIApplication sharedApplication]registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];

//

    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {

        

        [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings

                                                                             

                                                                             settingsForTypes:(UIUserNotificationTypeSound |

                                                                                               

                                                                                               UIUserNotificationTypeAlert |

                                                                                               

                                                                                               UIUserNotificationTypeBadge)

                                                                             

                                                                             categories:nil]];

        

        [[UIApplication sharedApplication] registerForRemoteNotifications];

        

    }

    

    else {

        

        [[UIApplication sharedApplication]registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert|

         

         UIRemoteNotificationTypeBadge|

         

         UIRemoteNotificationTypeSound]; 

        

    }

    


   

  if (launchOptions) {

        // UIApplicationLaunchOptionsRemoteNotificationKey 這個key值就是push的資訊

        NSDictionary *dic = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];

        // 為了複用代碼,統一到下面這個處理方法中handlePushNotify:

        [self handlePushNotify:dic fromBuld:_ISPushFromClosed];          //如果程式沒有啟動,點擊推送訊息進入程式,需要根據推送訊息做一些處理,如:視圖切換等。

    }

        

        

//註冊遠程推送,成功後的代理方法

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{

    

    //蘋果伺服器返回的token (蘋果伺服器用來標識手機的唯一編號)

    //可以交給後台,然後後台需要給此發送遠程推送的時候直接用這個token

    NSString *tokenStr = [deviceToken description];

    NSLog(@"token:%@",tokenStr);   

}


 

//註冊遠程推送失敗

-(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error

{

    NSLog(@"註冊推送失敗%@",error);

}


//程式正在運行時,點擊推送訊息所走的方法

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo

{

    //在這裡可以根據推送資訊做一些操作

    [self handlePushNotify:userInfo fromBuld:_ISPushFromOpen];

}


-(void)handlePushNotify:(NSDictionary *)dic fromBuld:(NSString *)buld

{

  // 根據buld判斷是哪裡傳來的值 

/*

 dic = {

 aps =     {

 alert = "\U627e\U5927\U592b\U63d0\U793a\Uff1ahdf21\U7528\U6237\U7533\U8bf7\U52a0\U5165\U60a8\U7684\U5c0f\U680b\U5708\U5708\U5b50";

 sound = default;

 target =         {

 "param_list" =             (

 {

 "param_name" = "circle_id";

 "param_value" = 2204;

 },

 {

 "param_name" = "circle_name";

 "param_value" = "\U5c0f\U680b\U5708";

 },

 {

 "param_name" = type;

 "param_value" = 1;

 }

 );

 topage = "/gooddoc/server/index.php/circle/getCircleMembersNews";

 };

 };

 */

}


本文出自 “8594233” 部落格,請務必保留此出處http://8604233.blog.51cto.com/8594233/1576178

ios8以上遠程推送 demo

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.