ios 訊息推送詳情,ios訊息推詳情

來源:互聯網
上載者:User

ios 訊息推送詳情,ios訊息推詳情

訊息推送流程

手機系統會一直保持與蘋果APNs一個通訊

當開啟App時候,會從蘋果Apns得到一個toKen

發送toKen到後台伺服器

伺服器發送這個toKen 和 推送資料給蘋果APNs (以為push通道是由蘋果維護的一個唯一的通道)

蘋果APNs得到後再根據這個toKen推送得到的訊息給我們的ios裝置

我們得到toKen後就知道是哪個APP的推送內容了

訊息推送(以V8.0+為例子)(搭建push環境)

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

{

//讀取系統版本,判斷是否大於8.0

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

//首先向使用者詢問push的許可權,向使用者申請許可權

UIUserNotificationSettings* setting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert categories:nil];

[[UIApplication sharedApplication] registerUserNotificationSettings:setting];

}else{

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

}

}

//使用者同意之後回調,去註冊DeviceToken

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(nonnull UIUserNotificationSettings *)notificationSettings{

[[UIApplication sharedApplication] registerForRemoteNotifications];

}

//註冊成功回調,得到DeviceToken

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

DLog(@“succeed regist remote notification");

NSString* tokenStr = [NSString stringWithFormat:@"%@", aDeviceToken];

tokenStr = [tokenStr stringByReplacingOccurrencesOfString:@" " withString:@""];

tokenStr = [tokenStr stringByReplacingOccurrencesOfString:@"<" withString:@""];

tokenStr = [tokenStr stringByReplacingOccurrencesOfString:@">" withString:@""];

//向伺服器發送

}

//註冊失敗回調

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

DLog(@"failed regist remote notification %@ ", error);

}

//當接收到遠程通知回調

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

NSDictionary *apsDictionary = [userInfo objectForKey:@"aps"];

//解析JSon

/*

具體處理。。。

if(...){

跳轉某個controller。。。

}

*/

}

訊息推送(本地推送)

@property (nonatomic, retain) UILocalNotification *localNotification;

- (void)backGround{

_localNotification = [UILocalNotification new];

//多久以後提示使用者

_localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];

_localNotification.alertBody = @"你都走了5秒鐘了";

_localNotification.alertTitle = @"你妹的!";

_localNotification.userInfo = @{@"key":@"value"};

[[UIApplication sharedApplication] scheduleLocalNotification:_localNotification];

}

- (void)foreGround{

[[UIApplication sharedApplication] cancelLocalNotification:_localNotification];

}

- (void)viewDidLoad

{

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(backGround) name:UIApplicationDidEnterBackgroundNotification object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(foreGround) name:UIApplicationWillEnterForegroundNotification object:nil];

}

相關文章

聯繫我們

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