點擊推送訊息跳轉處理(iOS)

來源:互聯網
上載者:User

標籤:

  當使用者點擊收到的推送訊息時候,我希望開啟APP,並且跳轉到對應的介面,這就需要在AppDelegate裡面對代理方法進行處理。

  當使用者點擊推送訊息開啟APP的時候會調用

- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions

  launchOptions中會有推送訊息的userInfo資訊,此時我們可以通過

NSDictionary* remoteNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];

  擷取到推送的內容;如果 remoteNotification 不為空白,則說明使用者是通過推送進入的APP,那麼可以聲明一個屬性

@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);}

 

 

  增加一個小技巧,與好友開視頻的推送,當應用到背景時候,也可能是被kill了,本人很奇怪,為什麼這個推播通知,聲音和震動可以不停下來,一直提醒使用者,而且iOS8上頂部的通知橫幅也是一直顯示,直到使用者點擊之後進入應用才會停止,這個是怎麼做到的?

  其實用一個小於30s的音頻檔案就搞定了

 

 

 

 

 

 

本文參考公眾號:iOS開發

  

點擊推送訊息跳轉處理(iOS)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.