當用戶點擊收到的推送消息時候,我但願打開APP,而且跳轉到對應的界面,這就須要在AppDelegate裏面對代理方法進行處理。微信
當用戶點擊推送消息打開APP的時候會調用app
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
launchOptions中會有推送消息的userInfo信息,此時咱們能夠經過函數
NSDictionary* remoteNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
獲取到推送的內容;若是 remoteNotification 不爲空,則說明用戶是經過推送進入的APP,那麼能夠聲明一個屬性fetch
@property (nonatomic) BOOL isLaunchedByNotification;
用於標識用戶是否經過點擊通知消息進入本應用。此時,atom
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo
必定會被調用,iOS7可使用spa
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
由於此方法的調用時,MainViewController已經被初始化,因此咱們已經能夠在MainViewController註冊推送消息的監聽,用於展現對應的視圖,以下:代理
//訂閱展現視圖消息,將直接打開某個分支視圖 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(presentView:) name:@"PresentView" object:nil];//彈出消息框提示用戶有訂閱通知消息。主要用於用戶在使用應用時,彈出提示框 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showNotification:) name:@"Notification" object:nil];
因此在AppDelegate的didReceiveRemoteNotification中能夠經過判斷isLaunchedByNotification來通知不一樣的展現方法。code
首先包含頭文件視頻
#import <AudioToolbox/AudioToolbox.h>
註冊一段聲音(本例中直接使用默認1007)server
@property (nonatomic, assign) SystemSoundID soundID; NSString *path = [[NSBundle mainBundle] pathForResource:soundName ofType:nil]; AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path], &_soundID); AudioServicesAddSystemSoundCompletion(_soundID, NULL, NULL, soundCompleteCallback, NULL); // 核心代碼 可重複執行 AudioServicesPlaySystemSound(kSystemSoundID_Vibrate); AudioServicesPlaySystemSound(_soundID); // block 用於 AudioServicesAddSystemSoundCompletion(_soundID, NULL, NULL, soundCompleteCallback, NULL); 函數調用 void soundCompleteCallback(SystemSoundID soundID,void * clientData) { AudioServicesPlaySystemSound(kSystemSoundID_Vibrate); AudioServicesPlaySystemSound(soundID); } // 中止播放 -(void)stopAlertSoundWithSoundID:(SystemSoundID)soundID { AudioServicesDisposeSystemSoundID(kSystemSoundID_Vibrate); AudioServicesDisposeSystemSoundID(soundID); AudioServicesRemoveSystemSoundCompletion(soundID); }
增長一個小技巧,微信與好友開視頻的推送,當微信應用到後臺的時候,也多是被kill了,本人很奇怪,爲何這個推送通知,聲音和震動能夠不停下來,一直提醒用戶,並且iOS8上頂部的通知橫幅也是一直顯示,直到用戶點擊以後進入微信應用纔會中止,這個是怎麼作到的?
其實用一個小於30s的音頻文件就搞定了
本文參考微信公衆號:iOS開發