didReceiveRemoteNotification: is not called after notification arrives and app is in background/suspended...












1















Within AppDelegate I simply update applicationIconBadgeNumber:



func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
application.applicationIconBadgeNumber += 1
}


Everything works as expected when the app is connected to Xcode and is in debugger mode. But just after I plug it out from Xcode, notification arrives but badge is not updated. App is in background mode.



Why? What is wrong with my approach? Please, give me an advice🏆










share|improve this question





























    1















    Within AppDelegate I simply update applicationIconBadgeNumber:



    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
    application.applicationIconBadgeNumber += 1
    }


    Everything works as expected when the app is connected to Xcode and is in debugger mode. But just after I plug it out from Xcode, notification arrives but badge is not updated. App is in background mode.



    Why? What is wrong with my approach? Please, give me an advice🏆










    share|improve this question



























      1












      1








      1


      1






      Within AppDelegate I simply update applicationIconBadgeNumber:



      func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
      application.applicationIconBadgeNumber += 1
      }


      Everything works as expected when the app is connected to Xcode and is in debugger mode. But just after I plug it out from Xcode, notification arrives but badge is not updated. App is in background mode.



      Why? What is wrong with my approach? Please, give me an advice🏆










      share|improve this question
















      Within AppDelegate I simply update applicationIconBadgeNumber:



      func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
      application.applicationIconBadgeNumber += 1
      }


      Everything works as expected when the app is connected to Xcode and is in debugger mode. But just after I plug it out from Xcode, notification arrives but badge is not updated. App is in background mode.



      Why? What is wrong with my approach? Please, give me an advice🏆







      ios swift push-notification






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 16 '18 at 10:29







      Bartłomiej Semańczyk

















      asked Nov 16 '18 at 6:58









      Bartłomiej SemańczykBartłomiej Semańczyk

      34.4k25170237




      34.4k25170237
























          4 Answers
          4






          active

          oldest

          votes


















          3














          Push notification are handled by iOS and not your app you can't change the application badge on receiving a push notification.
          You can send the badge number in the payload of the push notification,



          Payload could look like this:



          {
          "aps" : {
          "alert" : "Notification REceived",
          "badge" : 1
          }
          }





          share|improve this answer































            0














            You need to check in didReceiveRemoteNotification with fetchCompletionHandler of UNUserNotificationCenter delegate method



            func application(
            _ application: UIApplication,
            didReceiveRemoteNotification userInfo: [AnyHashable : Any],
            fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

            messagingManager?.appDidReceiveMessage(userInfo)
            }





            share|improve this answer
























            • I updated the question. My delegate method is not called when app is in background mode and not connected to the debugger;)

              – Bartłomiej Semańczyk
              Nov 16 '18 at 7:17











            • Have you confirm delegate UNUserNotificationCenter.current().delegate = self inside application(_:didFinishLaunchingWithOptions:) method

              – iParesh
              Nov 16 '18 at 7:18













            • Yes, I can confirm;) That method is called when app is connected to the debugger, but doesnt work when not connected to the debugger. Why?

              – Bartłomiej Semańczyk
              Nov 16 '18 at 7:21













            • What makes the difference with that two methods?;)

              – Bartłomiej Semańczyk
              Nov 16 '18 at 7:33



















            0














            I found this post very helpful for managing notifications in the background, basically you have to use the call with completion handler.



            didReceiveRemoteNotification when in background






            share|improve this answer































              -1














              Your messagingManager is an optional. You should check, that it isn't nil. If it is nil, the appDidReciveMessage() function would not be triggered






              share|improve this answer
























              • It is not nil;) it works when app is connected to the debugger

                – Bartłomiej Semańczyk
                Nov 16 '18 at 7:07











              • Did you check also that it isnt nil when your App is in Background?

                – Jonathan
                Nov 16 '18 at 7:10











              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53332889%2fdidreceiveremotenotification-is-not-called-after-notification-arrives-and-app-i%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              4 Answers
              4






              active

              oldest

              votes








              4 Answers
              4






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              3














              Push notification are handled by iOS and not your app you can't change the application badge on receiving a push notification.
              You can send the badge number in the payload of the push notification,



              Payload could look like this:



              {
              "aps" : {
              "alert" : "Notification REceived",
              "badge" : 1
              }
              }





              share|improve this answer




























                3














                Push notification are handled by iOS and not your app you can't change the application badge on receiving a push notification.
                You can send the badge number in the payload of the push notification,



                Payload could look like this:



                {
                "aps" : {
                "alert" : "Notification REceived",
                "badge" : 1
                }
                }





                share|improve this answer


























                  3












                  3








                  3







                  Push notification are handled by iOS and not your app you can't change the application badge on receiving a push notification.
                  You can send the badge number in the payload of the push notification,



                  Payload could look like this:



                  {
                  "aps" : {
                  "alert" : "Notification REceived",
                  "badge" : 1
                  }
                  }





                  share|improve this answer













                  Push notification are handled by iOS and not your app you can't change the application badge on receiving a push notification.
                  You can send the badge number in the payload of the push notification,



                  Payload could look like this:



                  {
                  "aps" : {
                  "alert" : "Notification REceived",
                  "badge" : 1
                  }
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 16 '18 at 7:37









                  JunaidJunaid

                  59110




                  59110

























                      0














                      You need to check in didReceiveRemoteNotification with fetchCompletionHandler of UNUserNotificationCenter delegate method



                      func application(
                      _ application: UIApplication,
                      didReceiveRemoteNotification userInfo: [AnyHashable : Any],
                      fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

                      messagingManager?.appDidReceiveMessage(userInfo)
                      }





                      share|improve this answer
























                      • I updated the question. My delegate method is not called when app is in background mode and not connected to the debugger;)

                        – Bartłomiej Semańczyk
                        Nov 16 '18 at 7:17











                      • Have you confirm delegate UNUserNotificationCenter.current().delegate = self inside application(_:didFinishLaunchingWithOptions:) method

                        – iParesh
                        Nov 16 '18 at 7:18













                      • Yes, I can confirm;) That method is called when app is connected to the debugger, but doesnt work when not connected to the debugger. Why?

                        – Bartłomiej Semańczyk
                        Nov 16 '18 at 7:21













                      • What makes the difference with that two methods?;)

                        – Bartłomiej Semańczyk
                        Nov 16 '18 at 7:33
















                      0














                      You need to check in didReceiveRemoteNotification with fetchCompletionHandler of UNUserNotificationCenter delegate method



                      func application(
                      _ application: UIApplication,
                      didReceiveRemoteNotification userInfo: [AnyHashable : Any],
                      fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

                      messagingManager?.appDidReceiveMessage(userInfo)
                      }





                      share|improve this answer
























                      • I updated the question. My delegate method is not called when app is in background mode and not connected to the debugger;)

                        – Bartłomiej Semańczyk
                        Nov 16 '18 at 7:17











                      • Have you confirm delegate UNUserNotificationCenter.current().delegate = self inside application(_:didFinishLaunchingWithOptions:) method

                        – iParesh
                        Nov 16 '18 at 7:18













                      • Yes, I can confirm;) That method is called when app is connected to the debugger, but doesnt work when not connected to the debugger. Why?

                        – Bartłomiej Semańczyk
                        Nov 16 '18 at 7:21













                      • What makes the difference with that two methods?;)

                        – Bartłomiej Semańczyk
                        Nov 16 '18 at 7:33














                      0












                      0








                      0







                      You need to check in didReceiveRemoteNotification with fetchCompletionHandler of UNUserNotificationCenter delegate method



                      func application(
                      _ application: UIApplication,
                      didReceiveRemoteNotification userInfo: [AnyHashable : Any],
                      fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

                      messagingManager?.appDidReceiveMessage(userInfo)
                      }





                      share|improve this answer













                      You need to check in didReceiveRemoteNotification with fetchCompletionHandler of UNUserNotificationCenter delegate method



                      func application(
                      _ application: UIApplication,
                      didReceiveRemoteNotification userInfo: [AnyHashable : Any],
                      fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

                      messagingManager?.appDidReceiveMessage(userInfo)
                      }






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 16 '18 at 7:14









                      iPareshiParesh

                      1,2551926




                      1,2551926













                      • I updated the question. My delegate method is not called when app is in background mode and not connected to the debugger;)

                        – Bartłomiej Semańczyk
                        Nov 16 '18 at 7:17











                      • Have you confirm delegate UNUserNotificationCenter.current().delegate = self inside application(_:didFinishLaunchingWithOptions:) method

                        – iParesh
                        Nov 16 '18 at 7:18













                      • Yes, I can confirm;) That method is called when app is connected to the debugger, but doesnt work when not connected to the debugger. Why?

                        – Bartłomiej Semańczyk
                        Nov 16 '18 at 7:21













                      • What makes the difference with that two methods?;)

                        – Bartłomiej Semańczyk
                        Nov 16 '18 at 7:33



















                      • I updated the question. My delegate method is not called when app is in background mode and not connected to the debugger;)

                        – Bartłomiej Semańczyk
                        Nov 16 '18 at 7:17











                      • Have you confirm delegate UNUserNotificationCenter.current().delegate = self inside application(_:didFinishLaunchingWithOptions:) method

                        – iParesh
                        Nov 16 '18 at 7:18













                      • Yes, I can confirm;) That method is called when app is connected to the debugger, but doesnt work when not connected to the debugger. Why?

                        – Bartłomiej Semańczyk
                        Nov 16 '18 at 7:21













                      • What makes the difference with that two methods?;)

                        – Bartłomiej Semańczyk
                        Nov 16 '18 at 7:33

















                      I updated the question. My delegate method is not called when app is in background mode and not connected to the debugger;)

                      – Bartłomiej Semańczyk
                      Nov 16 '18 at 7:17





                      I updated the question. My delegate method is not called when app is in background mode and not connected to the debugger;)

                      – Bartłomiej Semańczyk
                      Nov 16 '18 at 7:17













                      Have you confirm delegate UNUserNotificationCenter.current().delegate = self inside application(_:didFinishLaunchingWithOptions:) method

                      – iParesh
                      Nov 16 '18 at 7:18







                      Have you confirm delegate UNUserNotificationCenter.current().delegate = self inside application(_:didFinishLaunchingWithOptions:) method

                      – iParesh
                      Nov 16 '18 at 7:18















                      Yes, I can confirm;) That method is called when app is connected to the debugger, but doesnt work when not connected to the debugger. Why?

                      – Bartłomiej Semańczyk
                      Nov 16 '18 at 7:21







                      Yes, I can confirm;) That method is called when app is connected to the debugger, but doesnt work when not connected to the debugger. Why?

                      – Bartłomiej Semańczyk
                      Nov 16 '18 at 7:21















                      What makes the difference with that two methods?;)

                      – Bartłomiej Semańczyk
                      Nov 16 '18 at 7:33





                      What makes the difference with that two methods?;)

                      – Bartłomiej Semańczyk
                      Nov 16 '18 at 7:33











                      0














                      I found this post very helpful for managing notifications in the background, basically you have to use the call with completion handler.



                      didReceiveRemoteNotification when in background






                      share|improve this answer




























                        0














                        I found this post very helpful for managing notifications in the background, basically you have to use the call with completion handler.



                        didReceiveRemoteNotification when in background






                        share|improve this answer


























                          0












                          0








                          0







                          I found this post very helpful for managing notifications in the background, basically you have to use the call with completion handler.



                          didReceiveRemoteNotification when in background






                          share|improve this answer













                          I found this post very helpful for managing notifications in the background, basically you have to use the call with completion handler.



                          didReceiveRemoteNotification when in background







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 16 '18 at 16:51









                          MariaMaria

                          2,62411523




                          2,62411523























                              -1














                              Your messagingManager is an optional. You should check, that it isn't nil. If it is nil, the appDidReciveMessage() function would not be triggered






                              share|improve this answer
























                              • It is not nil;) it works when app is connected to the debugger

                                – Bartłomiej Semańczyk
                                Nov 16 '18 at 7:07











                              • Did you check also that it isnt nil when your App is in Background?

                                – Jonathan
                                Nov 16 '18 at 7:10
















                              -1














                              Your messagingManager is an optional. You should check, that it isn't nil. If it is nil, the appDidReciveMessage() function would not be triggered






                              share|improve this answer
























                              • It is not nil;) it works when app is connected to the debugger

                                – Bartłomiej Semańczyk
                                Nov 16 '18 at 7:07











                              • Did you check also that it isnt nil when your App is in Background?

                                – Jonathan
                                Nov 16 '18 at 7:10














                              -1












                              -1








                              -1







                              Your messagingManager is an optional. You should check, that it isn't nil. If it is nil, the appDidReciveMessage() function would not be triggered






                              share|improve this answer













                              Your messagingManager is an optional. You should check, that it isn't nil. If it is nil, the appDidReciveMessage() function would not be triggered







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Nov 16 '18 at 7:04









                              JonathanJonathan

                              1




                              1













                              • It is not nil;) it works when app is connected to the debugger

                                – Bartłomiej Semańczyk
                                Nov 16 '18 at 7:07











                              • Did you check also that it isnt nil when your App is in Background?

                                – Jonathan
                                Nov 16 '18 at 7:10



















                              • It is not nil;) it works when app is connected to the debugger

                                – Bartłomiej Semańczyk
                                Nov 16 '18 at 7:07











                              • Did you check also that it isnt nil when your App is in Background?

                                – Jonathan
                                Nov 16 '18 at 7:10

















                              It is not nil;) it works when app is connected to the debugger

                              – Bartłomiej Semańczyk
                              Nov 16 '18 at 7:07





                              It is not nil;) it works when app is connected to the debugger

                              – Bartłomiej Semańczyk
                              Nov 16 '18 at 7:07













                              Did you check also that it isnt nil when your App is in Background?

                              – Jonathan
                              Nov 16 '18 at 7:10





                              Did you check also that it isnt nil when your App is in Background?

                              – Jonathan
                              Nov 16 '18 at 7:10


















                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53332889%2fdidreceiveremotenotification-is-not-called-after-notification-arrives-and-app-i%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Xamarin.iOS Cant Deploy on Iphone

                              Glorious Revolution

                              Dulmage-Mendelsohn matrix decomposition in Python