Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
129 views
in Technique[技术] by (71.8m points)

What is difference between remote notification and silent notification in iOS?

When I read Apple Docs, they mention 3 types of notification: local, remote, and silent.

Local notification can be inferred from its name, that is sent by the app locally.

However, what is the difference between the other two types?

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

EDIT: While this answer is fully applicable, there are some additions (not changes) to notifications in iOS 12. I highly recommend watching WWDC 2018: What’s New in User Notifications and read this amazing and must read article.

Main changes are:

  • grouped notifications along with summary format
  • provisional notifications ie show notifications directly in notification center without user permission
  • critical notifications which ignore 'do not disturb' or 'mute'
  • ability to interact with the notifications in the extensions
  • ability to completely reset or update actions
  • ability to deeplink into app's notification Settings from phone's Notification Center

IMPORTANT NOTE: Not sure since when but from the Apple docs, the 'silent notification' has been renamed to 'background notification'

There are too many settings that need to be set right for it to work. I'll try to dissect them and make them easier to understand.

Overall, several things are important.

  • the overall difference between a silent and user notification
  • different types of user notifications
  • how a remote notification, i.e. the payload, is configured from your server
  • how to enable push notifications and remote notifications from background modes on your project
  • how to register your token with APNs for remote and silent notifications and APNs architecture
  • how to request permission for user notifications
  • enabling 'background app refresh' and 'notifications' from the device
  • what is content-available
  • understanding that the iOS is upstream to your app when it comes to receiving a remote notification
  • what happens when the OS receives notifications when the app has been user-terminated
  • A note on reliability and APNs architecture

I highly recommend everyone to watch the first 7 minutes of: WWDC 2015: What's new in Notifications. From there, the presenter mentions that there are 2 major types of notifications:

Silent Notifications

They happen in the background, hence you never see any alert/badge/sound. Things get downloaded without you knowing about them.

iOS 11 bug

See here. iOS 11 initial releases were buggy for silent notifications. Make sure you have the latest version for your testing, otherwise, it may not work


User Notifications

enter image description here

As the name says, it has something to do with the user. That is, the user will see an alert/badge or hear a sound. It has 2 types.

Local Notifications

A Local Notification can be triggered in 3 different ways:

  • UNLocationNotificationTrigger: You see an alert when you're close to a Walmart store.

  • UNTimeIntervalNotificationTrigger: e.g. You see an alert every 10 minutes.

  • UNCalendarNotificationTrigger like December 1st 1:00PM 2017.

Remote Notifications

They are similar to local notifications but they are triggered from the server, e.g. a WhatsApp message that has a From field (Mom) and a body field (I love you!).

Token registration and APNs architecture:

To receive a silent or remote notification, you need to register for a token using:

application.registerForRemoteNotifications() 

?? Registering does NOT require user permission. This makes silent notifications to become seamless. See this moment of the WWDC video

Silent notifications are enabled by default. The user does not need to approve your -- does not give permission to your app to use them, and you can just start using them without asking the user for permission.

From WWDC

Remember APNs is delivered to your users by APNs and not by your server. So your iOS code must send this token to your server. So the server can associate a given device token with the user. When you want to push to a certain user, your server just tells APNs to send a payload to a specific token. What's important to understand is that your server and APNs are two different things

The flow of it looks like this:

?

enter image description here

?

  1. server/provider sends a payload to APNs
  2. APNs send a notification to all target devices of a given account. e.g. your iPhone, Mac could both receive notifications for emails/messages.
  3. Then your iPhone/Mac will deliver that message to the app. APNs don't directly send messages to your app. It sends it to the device. Then the iOS sends it to your app.

For more on this see docs APNs Overview and Sending Notification Requests to APNs


To be able to show badges/alerts/sounds, you need to request permission from the user:

UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in
    
    guard error == nil else {
        //Display Error.. Handle Error.. etc..
        return
    }
    
    if granted {
        //Do stuff here..
        
        //Register for RemoteNotifications. Your Remote Notifications can display alerts now :)
        application.registerForRemoteNotifications()
    }
    else {
        //Handle user denying permissions..
    }
}

Question: Do I need to request access once for local notifications and once for remote notifications?

No. Just write the snippet above and it will request access for both remote and local.

Now let's get to the tricky part :D


Xcode Project + iPhone Settings

Do I need to enable something to receive silent notifications?

  1. You must enable Push Notifications from your Xcode capabilities:

enter image description here

If you don't enable this, your app won't receive a token. And without a token, the server doesn't recognize you.

  1. To be able to download stuff from the background, you need to enable: remote notifications from background modes.

To enable backgroundModes, you can do it either using your plist or Xcode capabilities.

The reason you can do it, either way, is because plist is closer to your code and is the old way, perhaps it's there for legacy support. Xcode capabilities is the newer, easy way.

plist:

enter image description here

Item 0 is just an index, it's not the key of a dictionary (something you normally see in plist), the UIBackgroundModes is an array of Strings. The strings must only come from an accepted value from the UIBackgroundModes Array.

Xcode Capabilities:

Check the Remote Notification in Xcode under background modes as below:

enter image description here

If you don't do any of the above, then toggling off notifications with:

enter image description here

will kill Remote & Local Notifications


However, if you do enable background app refresh from plist or Xcode capabilities, then even with notifications turned off for the app, you will still receive silent notifications!

If the user wants to disable silent notifications, he would have to disable both notifications and disable 'background app refresh for your app / across the system. To disable 'background app refresh' across your system, you have to do this:

enter image description here

Why am I saying all this? To explain to you that settings of silent and push notifications are different for the user and the restrictions for sending them are different. For more, see this moment from the WWDC video. See here instead (the previous link was dead):

Silent notifications are enabled by default.

The user does not need to approve your does not give permission to your app to use them, and you can just start using them without asking the user for permission.

But silent notifications are the mechanism behind background app refresh.

At any point you know that the user can go in settings and disable them.

So you can't depend on them always being available.

You don't know if the user the turn them off, and you are not getting a notification anymore.

This also means that silent notifications are delivered with the best effort.

That means that when the notification arrives on the user's device,


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...