Swift 3 - Turn off icon, alert, sound

I have read many answers here and I cannot find the answers to these questions How do I stop the icon, alert and sound from notifications when the app is not running? How do I stop the icon, alert and sound from notifications when the app is in the background?

in this function:

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)

      

I put completeHandler ([]) without [.badge, .alert, .sound] and I still get background notifications when the app starts.

Also I have a function in my code that sends a notification when someone prints you and you see it prints you when you are in the application, but when the application does not start you get an icon warning notification? How can this be prevented?

PHP code:

$body['aps'] = array(
    'content-available' => 0,
    'typingNewMessage' => 'true',
    'senderId' => $your_id
    );

      

+3


source to share


3 answers


Two things first, are you using websocket / socket.io for chat in your application? As for notifications, have you set up remote notifications in the background?

Background modes.

Read the docs for push notifications , it looks like you are adding steps here or missing. Basically, as kathayatnk said, his method should give you what you want. Therefore, if you have not activated remote background notifications:

To configure the app to handle silent notifications in the background

  • In the Project Navigator, select your project.
  • In the editor, select the target for the iOS app.
  • Select the "Features" tab.
  • Enable background capabilities.
  • Enable background mode for remote notifications.

If this is done, you should get them in the background.

Also, you are using the wrong method to receive background notifications in the background. As stated in the same link above:

On iOS, the system provides silent notifications by calling the app: didReceiveRemoteNotification: fetchCompletionHandler : method of your app delegate. Use this method to initiate any download operation required to retrieve new data.

how I usually use it:

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
   guard let aps = userInfo["aps"] as? [String:Any] else {
      //handle something here..
      return 
   }

   //handle the content here...

}

      



Here, you can also perform background tasks from inactive states by playing around with the completeHandler return value . I also suggest you read about it.

For your chat problem ...

Clarify how you do it ...

If you are using websocket / socket.io architecture:

I would highly recommend not using notifications to notify the user of anything with APNS when using websocket. Websockets are very efficient and much faster. Handling both can complicate matters considerably. Stick with the socket approach and use the server side of the method that checks if the user is in the application. Something like ping pong like here . Ask this method to switch to APNS delivery when the ping method of the socket client shows that the user has left the application or closed it. This means that if the user is still in the app but not in a conversation, stay with the ping approach, but program the UI response differently (for example, use the FB Messenger or Snapchat notification tabs).

If you are not using websockets / socket.io:

... Switch to technologies or website packages (depending on your server platform type) or socket.io or use firebase realtime chat services .

Let me know your setup as I asked before, but if you are doing all of this already and still getting problems then post your code.

Personnaly, I have used RawWenderlicht to guide APNS for over a year, good stuff if you ask me.

+1


source


B silent

push notification

payload

must contain a key content-available

like:

{
  "aps": {
    "content-available": 1
  }
}

      

Also, you need to enable Remote Notifications

in Background Modes

in Capabilities

your project settings Xcode

.

enter image description here



Now yours app will wake up in the background

when he gets one of these push notifications

.

didReceiveRemoteNotification

will be called in AppDelegate

upon reception push notification

.

func application(
        _ application: UIApplication,
        didReceiveRemoteNotification userInfo: [AnyHashable : Any],
        fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void)
    {
        let aps = userInfo["aps"] as! [String: AnyObject]
        if aps["content-available"] as? Int == 1
        {
            //Your code
        }
    }

      

More details on how to handle foreground / background push notifications refer to: https://www.raywenderlich.com/156966/push-notifications-tutorial-getting-started

+1


source


you can add silent push notifications (set content-accessible key accordingly) when pushing as shown below.

//when you want the badge and sound
 aps {
   content-available: 1
 }

//When you want silent notifications
 aps {
   content-available: 0
 }

      

This will call the corresponding delegate method, but no icon and sound will be displayed

-1


source







All Articles