iOS-点击推送消息跳转处理

当用户通过点击通知消息进入应用时

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



 
posted @ 2016-04-12 10:17  jgCho  阅读(512)  评论(0编辑  收藏  举报