當使用者通過點擊通知訊息進入應用時
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
launchOptions
中會有推送訊息的userInfo資訊,此時我們可以通過
NSDictionary* remoteNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
獲得推送訊息內容。如果remoteNotification不為空白,則說明使用者通過推送訊息進入,那麼可以聲明一個屬性
@property (nonatomic) BOOL isLaunchedByNotification;
用於標識使用者是否通過點擊通知訊息進入本應用。此時,
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo
一定會被調用,iOS7可以使用
- (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來通知不同的展示方法。
腦補一個推送來臨時,播放震動聲音不停止的代碼(不是播放音樂哦)
首先包含標頭檔
#import <AudioToolbox/AudioToolbox.h>
註冊一段聲音(本例中直接使用預設1007)
@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);}
以上內容是小編給大家介紹的iOS點擊推送訊息跳轉處理的相關內容,希望對大家有所協助!