簡體   English   中英

如何處理點擊推送通知 ios

[英]How to handle on click on push notifications ios

我設法收到推送通知,但我不知道如何處理到達應用程序的推送通知上的點擊,我想根據通知類型告訴應用程序何時點擊 go 進行特定活動,而不僅僅是打開應用程序(默認行為)

我知道我可以收到

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{

    NSLog(@"Here I should Recive The notification .... ") ;
    //call the commented functions .....
}

在這個 function 中,但是當我執行它時,通知不會出現,只是在這個 function 中執行操作

對於在 2022 年遇到此問題的任何人,這是一個可行的解決方案。 連接推送通知服務后,您需要在 AppDelegate 中實現 didReceive function 並獲取通知內容:

func userNotificationCenter(_ center: UNUserNotificationCenter,
                            didReceive response: UNNotificationResponse,
                            withCompletionHandler completionHandler: @escaping () -> Void) {
    let userInfo = response.notification.request.content.userInfo
    let title = response.notification.request.content.title
    let message = response.notification.request.content.body
    userDefaultsManager.savePushNotificationData(pushNotificationData: ["title" : title, "message" : message])
}

請注意,如果您需要在推送本身中獲取自定義數據,它將存儲在 userInfo 中。 你可以這樣得到它:

func userNotificationCenter(_ center: UNUserNotificationCenter,
                        didReceive response: UNNotificationResponse,
                        withCompletionHandler completionHandler: @escaping () -> Void) {
     let userInfo = response.notification.request.content.userInfo
     let url = userInfo["url"]
}

嘗試處理此點擊,如代碼所示,

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

//Accept push notification when the app is not open.
        if (remoteNotifiInfo) {
            [self application:application didReceiveRemoteNotification: remoteNotifiInfo];
        }

}



   // On click of notification open related screen.

像這樣覆蓋didReceiveRemoteNotification

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
        {
                    UIApplicationState state = [[UIApplication sharedApplication] applicationState];
                    if ( state == UIApplicationStateInactive )
                    {
                        //Your actions on notification click
                        double delayInSeconds = 0.3;
                        dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
                        dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
                            //code to be executed on the main queue after delay
                            [[NSNotificationCenter defaultCenter] postNotificationName:@"pushTOEventDetails" object:eventVO];
                        });

                    }
        }

如果您想通過點擊pushnotification來導航某些特定的ViewController,只需在didReceiveRemoteNotification中添加ViewController導航代碼

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{

NSLog(@"Here I should Receive The notification .... ") ;
//call the commented functions .....

FirstController *firstviewcontroller = [[FirstController alloc] initWithNibName:@"FirstController" bundle:nil];

    [self.navigationController pushviewcontroller:firstviewcontroller animated:YES];

}

僅當應用程序處於活動狀態時,該委托方法才會被觸發。 如果應用程序未運行,則應該使用委托方法application:didFinishLaunchingWithOptions:中的launchOptions獲取通知有效負載,並根據有效負載進行導航。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM