博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
APN 推送
阅读量:5165 次
发布时间:2019-06-13

本文共 4091 字,大约阅读时间需要 13 分钟。

 

推送的各种状态

http://samwize.com/2015/08/07/how-to-handle-remote-notification-with-background-mode-enabled/

 

 

2 min read

iOS push notification has evolved greatly over the years, from a simple push with a alert string, to the now sophisticated system involving custom actions, localized string and background mode.

Reading the documentations ,  and ... provided clues everywhere on how it works.

This post is to explain what happens when a push reaches a device.

Which delegate method is called?

That is the question to answer when a push reaches a device.

Three possible callbacks:

  1. application:didReceiveRemoteNotification:fetchCompletionHandler:
  2. application:didFinishLaunchingWithOptions:
  3. No callback

There are , but we leave omit them in this discussion unless it is needed. Specifically they are:

  • application:didReceiveRemoteNotification: - This is the original API when push notification service is launched (iOS 3.0), but now it is superceded byapplication:didReceiveRemoteNotification:fetchCompletionHandler: (iOS 7.0).
  • application:handleActionWithIdentifier:forRemoteNotification:completionHandler:- This is for providing custom action that goes with a push notification (iOS 8.0). An advanced topic we shelf off for now.

Configuring The Payload & Silent Push

The  consists of:

  • alert - the alert string and actions
  • badge
  • sound
  • content-available

The key content-available is a new feature, and it is this key that makes silent pushpossible.

To enable, you also have to add remote-notifcation as your appUIBackgroundModes as described .

This is what happens when content-available is in the payload:

  • If app is Suspended, the system will bring it into Background
  • If app was killed by user, nothing happens and app remains in Not Running

Read about .

A potential is pitfall:

You enable with content-available=1. But, it is WRONG to disable with content-available=0. To disable, you have to REMOVE the key in the payload.

Playing out the Scenarios

With content-available enabled:

  1. App is in Foreground
    • No system alert shown
    • application:didReceiveRemoteNotification:fetchCompletionHandler: is called
  2. App is in Background
    • System alert is shown
    • application:didReceiveRemoteNotification:fetchCompletionHandler: is called
  3. App is in Suspended
    • App state changes to Background
    • System alert is shown
    • application:didReceiveRemoteNotification:fetchCompletionHandler: is called
  4. App is Not Running because killed by user
    • System alert is shown
    • No callback is called

With content-available disabled (key removed):

  1. App is in Foreground
    • No system alert shown
    • application:didReceiveRemoteNotification:fetchCompletionHandler: is called
  2. App is in Background or Suspended
    • System alert is shown
    • No method is called, but when user tap on the push and the app is opened
      • application:didReceiveRemoteNotification:fetchCompletionHandler:is called
  3. App is in Not Running
    • System alert is shown
    • No method is called, but when user tap on the push and the app is opened
      • application:didFinishLaunchingWithOptions: thenapplication:didReceiveRemoteNotification:fetchCompletionHandler:are both called

Conclusion

If app is in Foreground, the system will not show the alert and it is up to the app to display some UI afterapplication:didReceiveRemoteNotification:fetchCompletionHandler: is called.

Enabling content-available will put the app in Background (unless app was killed by user) and then callapplication:didReceiveRemoteNotification:fetchCompletionHandler:immediately.

Whereas without content-available, app will remain in Suspended, andapplication:didReceiveRemoteNotification:fetchCompletionHandler: is delayed until the app is opened.

Lastly, application:didFinishLaunchingWithOptions: is called only when the app is Not Running, and the user tap on the push alert. It will subsequently callapplication:didReceiveRemoteNotification:fetchCompletionHandler:. Therefore, it is better to handle the push inapplication:didReceiveRemoteNotification:fetchCompletionHandler:, as that covers all scenarios.

转载于:https://www.cnblogs.com/studyNT/p/5198431.html

你可能感兴趣的文章
windows下的C++ socket服务器(4)
查看>>
css3 2d转换3d转换以及动画的知识点汇总
查看>>
【Java】使用Eclipse进行远程调试,Linux下开启远程调试
查看>>
对Vue为什么不支持IE8的解释之一
查看>>
计算机改名导致数据库链接的诡异问题
查看>>
Java8内存模型—永久代(PermGen)和元空间(Metaspace)(转)
查看>>
ObjectiveC基础教程(第2版)
查看>>
centos 引导盘
查看>>
Notes of Daily Scrum Meeting(12.8)
查看>>
Apriori算法
查看>>
onlevelwasloaded的调用时机
查看>>
求出斐波那契数组
查看>>
lr_start_transaction/lr_end_transaction事物组合
查看>>
CodeIgniter学习笔记(四)——CI超级对象中的load装载器
查看>>
.NET CLR基本术语
查看>>
ubuntu的home目录下,Desktop等目录消失不见
查看>>
建立,查询二叉树 hdu 5444
查看>>
[Spring框架]Spring 事务管理基础入门总结.
查看>>
2017.3.24上午
查看>>
Python-常用模块及简单的案列
查看>>