- (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";
};
};
*/
}