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];
}