iOS点击推送消息跳转处理
程序员文章站
2023-11-26 16:57:10
当用户通过点击通知消息进入应用时
- (bool)application:(uiapplication*)application didfinishlaunchi...
当用户通过点击通知消息进入应用时
- (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点击推送消息跳转处理的相关内容,希望对大家有所帮助!