应用程序终止时处理推送通知

Has*_* D. 34 iphone objective-c push-notification ios

当我的应用程序没有运行并收到推送通知时,如果我点击该通知,则启动应用程序 - 但是它不会提示用户设置的警报视图,询问他们是否要查看通知的内容与否.它只是发射,并坐在那里.

当应用程序运行时,推送通知可以正常运行 - 无论是作为活动应用程序还是在后台运行 - 但是当应用程序未运行时,无法正常工作.

我尝试在application:didFinishLaunchingWithOptions中注销launchOptions NSDictionary:看看它带来了什么加载 - 但它出现了"(null)".所以它基本上什么都没有 - 这是没有意义的,因为它不应该包含Notification的负载?

任何人都有任何想法如何在应用程序未运行时如何使Push Notifications工作?

我的意思是当应用程序处于未运行状态时如何处理推送通知.如果您收到许多通知并且未打开应用程序,那么您也没有点击系统的通知面板.你是如何保留这些推动以便以后检索的.

Rah*_*tel 36

1) 当应用程序在后台运行时,当应用程序在前台运行时, application:didReceiveRemoteNotification:方法将调用如下.

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    if (application.applicationState == UIApplicationStateInactive)
    {
        // opened from a push notification when the app was on background
        NSLog(@"userInfo->%@", [userInfo objectForKey:@"aps"]);
    }
    else if(application.applicationState == UIApplicationStateActive)
    {
        // a push notification when the app is running. So that you can display an alert and push in any view
        NSLog(@"userInfo->%@", [userInfo objectForKey:@"aps"]);
    }
}
Run Code Online (Sandbox Code Playgroud)

2) 当应用程序未启动(接近)然后application:didFinishedLaunchingWithOptions方法将被调用.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    if (launchOptions != nil)
    {
        // opened from a push notification when the app is closed
        NSDictionary* userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
        if (userInfo != nil)
        {
             NSLog(@"userInfo->%@", [userInfo objectForKey:@"aps"]);
        }
    }
    else
    {
        // opened app without a push notification.
    }
}
Run Code Online (Sandbox Code Playgroud)

3)无法删除特定通知.如果用户从其中一个应用程序打开应用程序时,从应用程序中删除所有通知以便它们不显示在通知中心的方法是将应用程序徽章设置为0.

  • 我在/sf/ask/2636105321/找到了一些对我有用的东西 (2认同)

Viz*_*llx 10

根据您的问题,当您打开应用程序时无法保留所有通知,更好的是,您可以调用api按照后端/服务器的时间戳获取所有通知,这就是Facebook的工作方式.


Jav*_*ría 7

该应用程序不会在后台处理推送通知,一旦您按下通知,操作系统就会唤醒应用程序.您可以通过以下方式捕捉到这一刻:

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

        if ([launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]) {
      // Your app has been awoken by a notification...
        }
   }
Run Code Online (Sandbox Code Playgroud)


nyg*_*nyg 6

您可以使用getDeliveredNotifications(completionHandler:)方法检索传递到应用程序的通知。请注意,这仅返回当前显示在“通知中心”中的通知,而不返回用户手动清除的通知。

UNUserNotificationCenter.current().getDeliveredNotifications { notifications in

    // notifications: An array of UNNotification objects representing the local
    // and remote notifications of your app that have been delivered and are still
    // visible in Notification Center. If none of your app’s notifications are
    // visible in Notification Center, the array is empty.

    // As said in the documentation, this closure may be executed in a background
    // thread, so if you want to update your UI you'll need to do the following:
    DispatchQueue.main.sync { /* or .async {} */ 
        // update UI
    }
}
Run Code Online (Sandbox Code Playgroud)