IOS推送訊息處理跳轉指定頁面
阿新 • • 發佈:2019-02-08
APP有UITabbarController、UINavigationController;
主頁:FirstViewController
指定頁:MyViewController
@interfaceCustomTabBarViewCtr:UITabBarController
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo;"><span style="color: rgb(222, 56, 166);">@interface</span><span style="color:#009900;"> CustomNavigationViewCtr : </span><span style="color:#3333ff;">UINavigationController</span></p>
後臺執行時:
<pre name="code" class="objc"><span style="color:#006600;">// ios7 later 點選推送訊息 呼叫此方法 </span><span style="color:#3333ff;"> - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { application.applicationIconBadgeNumber = 0; [self getCurrentRootViewController:application]; completionHandler(UIBackgroundFetchResultNoData); }</span>
程式關閉時:<pre name="code" class="objc"><pre name="code" class="objc"><span style="color:#006600;">// 獲取當前viewcontroller</span> <span style="color:#3333ff;">-(void)getCurrentRootViewController:(UIApplication*)applocation { CustomTabBarViewCtr * instance = [CustomTabBarViewCtr shareTabBarViewCtr]; UIViewController *controller = instance.selectedViewController; if ([controller isKindOfClass:[UINavigationController class]]){ controller = [(UINavigationController *)controller visibleViewController]; MyMesgesViewController *five=[[MyMesgesViewController alloc]init]; [controller.navigationController pushViewController:five animated:YES]; } }</span>
<pre name="code" class="objc"><span style="color:#009900;">//程式在關閉狀態時 點選推送訊息開啟程式則 launchOptions 不為空</span>
<span style="color:#3333ff;">-(void)zhuceTuiSOng:(UIApplication *)application WithOptions:(NSDictionary *)launchOptions
{
......其他程式碼......
if (launchOptions) {
application.applicationIconBadgeNumber = 0;
/* 想處理的程式碼寫在這 */
//擷取apns推送的訊息
NSDictionary* pushInfo = [launchOptions objectForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"];
//獲取推送詳情
NSString *pushInfostr = [NSString stringWithFormat:@"%@",[pushInfo objectForKey:@"aps"]];
}
}</span>
<span style="color:#009900;">會自動呼叫方法- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler;</span>
</pre><br /><br /><p><br /></p><p>// 附屬方法,這是一些其他類裡的方法,為了方便理解上邊程式碼,單獨拷貝出來以供參考理解</p><p></p><pre name="code" class="objc"><span style="color:#3333ff;">+(CustomTabBarViewCtr *)shareTabBarViewCtr
{
static CustomTabBarViewCtr * instance = nil;
if (instance == nil) {
instance = [[CustomTabBarViewCtr alloc] init];
}
return instance;
}
</span>
<span style="color:#3333ff;">-(void)addViewCtrs
{
FirstViewController * fvc = [[FirstViewController alloc] init];
CustomNavigationViewCtr * cvc1 = [[CustomNavigationViewCtr alloc] initWithRootViewController:fvc];
SecondViewController * fvc2 = [[SecondViewController alloc] init];
CustomNavigationViewCtr * cvc2 = [[CustomNavigationViewCtr alloc] initWithRootViewController:fvc2];
NSArray * aryTemp = [NSArray arrayWithObjects:cvc1,cvc2, nil];
self.viewControllers = aryTemp;
}</span>
評論是一種美德,是對作者的一種鼓勵,歡迎斧正,謝謝!