收到静默推送通知后,执行几行代码,应用程序在后台运行,ios

时间:2022-11-03 08:53:38
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary     *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
    NSLog(@"PUSH NOTIFICATION %@",userInfo);


    if([userInfo[@"aps"][@"content-available"] intValue] == 1){

        NSLog(@"SILENT PUSH NOTIFICATION");

        //Here I'm inserting a flag value to my DB.

        completionHandler(UIBackgroundFetchResultNewData);

    }
    else{

        NSLog(@" GENERAL PUSH NOTIFICATION ");

        completionHandler(UIBackgroundFetchResultNoData);

    }
}

My silent notification payload is {
        aps =     {
            "content-available" = 1;
            sound = "";
        };
    }

To support I have added a key in info.plist in "Required background modes"

为了支持我在“所需的后台模式”中添加了info.plist中的一个键

item 0 = App downloads content in response to push notifications

item 0 =应用程序下载内容以响应推送通知

Also in capabilities section my Background Modes are ON with remote notification check mark is selected.

此外,在功能部分,我的背景模式为ON,并选择了远程通知复选标记。

Now my question is when I run my app then I'm able to receive silent as well as general push notification and code is executing successfully in foreground mode but when I press home button (not forcefully quitting by swiping it away and also device's passcode lock is open) my app is going to background mode and then my code is not executing even I'm able to receive push notifications those are having alert and sound, but app is not launching in background mode.

现在我的问题是,当我运行我的应用程序然后我能够接收静音以及一般推送通知并且代码在前台模式下成功执行但是当我按下主页按钮时(不是通过轻扫它而不是强行退出以及设备的密码锁定)是开放的)我的应用程序进入后台模式,然后我的代码没有执行,即使我能够接收有警报和声音的推送通知,但应用程序不在后台模式下启动。

I'm thinking that for foreground and for background mode whenever my app is receiving any push notification don't matter is it silent or general push notification my first log in the delegate method should have to print on the console i.e NSLog(@"PUSH NOTIFICATION %@",userInfo);

我认为,对于前台和后台模式,无论何时我的应用程序正在接收任何推送通知,无论是静音还是一般推送通知我在委托方法中的第一次登录都必须在控制台上打印即NSLog(@“PUSH NOTIFICATION%@“,userInfo);

Please help I'm struggling with this since last 2-3 days.

请帮助我自从过去2-3天以来一直在努力。

My info.plist 收到静默推送通知后,执行几行代码,应用程序在后台运行,ios

我的info.plist

2 个解决方案

#1


0  

I am doing almost exactly the same thing as you except I use the silent push notifications to present a local notification when the app is in the background and use it to update some state when the app is in the foreground. I am NOT registering for background fetch, only "remote-notification".

我正在做几乎与你完全相同的事情,除了我使用静音推送通知在应用程序在后台时呈现本地通知,并在应用程序位于前台时使用它来更新某些状态。我没有注册后台获取,只是“远程通知”。

@Paulw11 is correct that you need to call the completionHandler each time didReceiveRemoteNotification is called.

@Paww11是正确的,每次调用didReceiveRemoteNotification时都需要调用completionHandler。

I would check your plist to make sure your background modes are configured correctly. Maybe you could post the contents of your plist.

我会检查你的plist以确保你的背景模式配置正确。也许你可以发布你的plist的内容。

My notification payload looks exactly the same as yours so I don't think that is the problem. I initially had issues because the payload wasn't exactly right.

我的通知有效负载与您的通知负载完全相同,所以我不认为这是问题所在。我最初有问题,因为有效载荷并不完全正确。

Keep in mind that if you kill the app (swipe it away), you will not get silent push notifications until the app is restarted.

请记住,如果您终止应用程序(将其轻扫),则在重新启动应用程序之前,您将无法获得静默推送通知。

#2


0  

I think you need to test once with 'content-available' outside 'aps', like the following:

我认为您需要在'aps'之外的'content-available'中测试一次,如下所示:

aps = {
            alert = "This is test alert";
            badge = 1;
       };
"content-available" = 1;

#1


0  

I am doing almost exactly the same thing as you except I use the silent push notifications to present a local notification when the app is in the background and use it to update some state when the app is in the foreground. I am NOT registering for background fetch, only "remote-notification".

我正在做几乎与你完全相同的事情,除了我使用静音推送通知在应用程序在后台时呈现本地通知,并在应用程序位于前台时使用它来更新某些状态。我没有注册后台获取,只是“远程通知”。

@Paulw11 is correct that you need to call the completionHandler each time didReceiveRemoteNotification is called.

@Paww11是正确的,每次调用didReceiveRemoteNotification时都需要调用completionHandler。

I would check your plist to make sure your background modes are configured correctly. Maybe you could post the contents of your plist.

我会检查你的plist以确保你的背景模式配置正确。也许你可以发布你的plist的内容。

My notification payload looks exactly the same as yours so I don't think that is the problem. I initially had issues because the payload wasn't exactly right.

我的通知有效负载与您的通知负载完全相同,所以我不认为这是问题所在。我最初有问题,因为有效载荷并不完全正确。

Keep in mind that if you kill the app (swipe it away), you will not get silent push notifications until the app is restarted.

请记住,如果您终止应用程序(将其轻扫),则在重新启动应用程序之前,您将无法获得静默推送通知。

#2


0  

I think you need to test once with 'content-available' outside 'aps', like the following:

我认为您需要在'aps'之外的'content-available'中测试一次,如下所示:

aps = {
            alert = "This is test alert";
            badge = 1;
       };
"content-available" = 1;