簡體   English   中英

推送通知 -didFinishLaunchingWithOptions

[英]Push Notification -didFinishLaunchingWithOptions

當我發送推送通知並且我的應用程序處於打開狀態或在后台並單擊推送通知時,我的應用程序重定向到PushMessagesVc viewController按預期

為此,我使用以下代碼:

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    UIStoryboard *mainstoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    PushMessagesVc *pvc = [mainstoryboard instantiateViewControllerWithIdentifier:@"PushMessagesVc"];

    [self.window.rootViewController presentViewController:pvc
                                                 animated:YES
                                               completion:NULL];
}

上面的代碼/場景沒有問題,但是如果應用程序關閉並且我單擊推送通知,則在這種情況下應用程序不會重定向我的PushMessagesVc viewController PushMessagesVc應用程序停留在主屏幕上。

對於第二個場景,我使用以下代碼:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    sleep(1);
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeNone)];
    [UIApplication sharedApplication].applicationIconBadgeNumber = 1;

    NSDictionary *userInfo = [launchOptions valueForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"];
    NSDictionary *apsInfo = [userInfo objectForKey:@"aps"];

    if(apsInfo) {
        UIStoryboard *mainstoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
        PushMessagesVc* pvc = [mainstoryboard instantiateViewControllerWithIdentifier:@"PushMessagesVc"];
        [self.window.rootViewController presentViewController:pvc animated:YES completion:NULL];
        return YES;
    }
    return YES;
}

但在這種情況下, PushMessagesVc不會出現。

由於您只想在收到推送通知時呈現一個viewController ,因此您可以嘗試使用NSNotificationCenter來實現您的目的:

第 1 部分:設置一個類(在您的例子中是rootViewController )來監聽/響應NSNotification

假設MainMenuViewController是您的navigationControllerrootViewController
設置這個類來監聽NSNotification

- (void)viewDidLoad {
    //...
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(presentMyViewOnPushNotification)
                                                 name:@"HAS_PUSH_NOTIFICATION"
                                               object:nil];
}

-(void)presentMyViewOnPushNotification {
    //The following code is no longer in AppDelegate
    //it should be in the rootViewController class (or wherever you want)

    UIStoryboard *mainstoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    PushMessagesVc *pvc = [mainstoryboard instantiateViewControllerWithIdentifier:@"PushMessagesVc"];

    [self presentViewController:pvc animated:YES completion:nil];
    //either presentViewController (above) or pushViewController (below)
    //[self.navigationController pushViewController:pvc animated:YES];
}

第 2 部分:發布通知(可以在代碼中的任何位置

在您的情況下, AppDelegate.m方法應如下所示

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //firstly, don't sleep the thread, it's pointless
    //sleep(1); //remove this line

    if (launchOptions) { //launchOptions is not nil
        NSDictionary *userInfo = [launchOptions valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
        NSDictionary *apsInfo = [userInfo objectForKey:@"aps"];

        if (apsInfo) { //apsInfo is not nil
            [self performSelector:@selector(postNotificationToPresentPushMessagesVC) 
                       withObject:nil
                       afterDelay:1];
        }
    }
    return YES;
}

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    //this method can be done using the notification as well
    [self postNotificationToPresentPushMessagesVC];
}

-(void)postNotificationToPresentPushMessagesVC {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"HAS_PUSH_NOTIFICATION" object:nil];
}

PS:我還沒有為我的項目做過這個()但它有效,是我能想到的最好的方式來做這件事(目前

斯威夫特 5

當應用程序被終止並收到推送通知並用戶單擊它時,在didFinishLaunchingWithOptions 中獲取推送通知字典

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    if let userInfo = launchOptions?[UIApplication.LaunchOptionsKey.remoteNotification] as? [String: AnyObject] {
        if let aps1 = userInfo["aps"] as? NSDictionary {
            print(aps1)
        }
    }
    return true
}

推送通知字典將顯示在警報中。

Swift 2.0 用於“未運行”狀態(本地和遠程通知)

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {


// Handle notification
if (launchOptions != nil) {

    // For local Notification
    if let localNotificationInfo = launchOptions?[UIApplicationLaunchOptionsLocalNotificationKey] as? UILocalNotification {

        if let something = localNotificationInfo.userInfo!["yourKey"] as? String {

            self.window!.rootViewController = UINavigationController(rootViewController: YourController(yourMember: something))
        }


    } else

    // For remote Notification
    if let remoteNotification = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as! [NSObject : AnyObject]? {

        if let something = remoteNotification["yourKey"] as? String {
            self.window!.rootViewController = UINavigationController(rootViewController: YourController(yourMember: something))
        }
    }

}


return true

}

斯威夫特 5

我將我的通知寫入 didFinishLaunchingWithOptions 中的 var "notification",如果 "notification" != nil 在 applicationDidBecomeActive 中開始我的推送,然后刪除他 (= nil)

class AppDelegate: UIResponder, UIApplicationDelegate {

var notification: [AnyHashable : Any]? = nil

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

    registerForPushNotifications()

    //write notification to var
    if launchOptions?[UIApplication.LaunchOptionsKey.remoteNotification] != nil {
        self.notification = launchOptions![UIApplication.LaunchOptionsKey.remoteNotification] as? [AnyHashable : Any]
    }

    return true
}

func applicationDidBecomeActive(_ application: UIApplication) {
    if notification != nil {
    //for launch notification after terminate app
        NotificationCenter.default.post(name: Notification.Name(rawValue: "startPush"), object: nil)
        notification = nil
    }
}

迅捷版:

   if let localNotification: UILocalNotification = launchOptions?[UIApplicationLaunchOptionsLocalNotificationKey] as? UILocalNotification { //launchOptions is not nil
        self.application(application, didReceiveLocalNotification: localNotification)
    }

暫無
暫無

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

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