當用戶通過點擊通知消息進入應用時
- (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點擊推送消息跳轉處理的相關內容,希望對大家有所幫助!