接收时执行 iOS 推送通知代码 [英] iOS Push Notification code execution on receive

查看:27
本文介绍了接收时执行 iOS 推送通知代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 iOS 开发消息传递应用程序.

I am developing messaging app for iOS.

我将用户的电话号码保存在服务器上,但我不会在后端保存联系人姓名.

I keep users' phone numbers to the server but I don't keep contact names on the backend side.

当用户收到来自服务器的推送通知时,服务器在通知负载中向用户发送电话号码和消息内容.我实现了 application:didReceiveRemoteNotification:fetchCompletionHandler 方法来显示本地通知.我在本地数据库中查找 phoneContactName:phoneNumber 对并以以下格式显示本地通知:%phoneContactName%:%messageContent%

When the user receives push notification from the server, server sends phone number and message content to the user in notification payload. I implemented application:didReceiveRemoteNotification:fetchCompletionHandler method to show local notification. I look up in local database for phoneContactName:phoneNumber pair and show local notification in the format: %phoneContactName%:%messageContent%

然而,障碍是我现在不知道如何在应用程序未运行时执行此代码.我试图实现 application:didFinishLaunchingWithOptions 并检查那里的

However, the hurdle is that I don't now how to execute this code when the app is not running. I tried to implement application:didFinishLaunchingWithOptions and check there for

NSDictionary *userInfo = [launchOptions valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey];

但未调用此代码.

所以为了解决这个问题,我想问你我应该使用什么架构模型.我应该在服务器端保留联系人姓名以从服务器端发送带有联系人姓名的notification.alert(到目前为止我的服务器不知道联系人电话号码的名称)还是有什么方法可以在应用程序时执行代码没有运行?

So to solve the problem I would like to ask you what architectural model should I use. Should I keep contact names on the server side to send notification.alert from the server side with the name of contact (so far my server doesn't know the name of contact phone number) or is there any way to execute code when the app is not running?

附言此外,我在收到通知时计算客户端的徽章编号.所以,如果可能的话,代码执行方案会更好.

P.S. Also, I calculate the badge number on the client side when notification is received. So, the code execution solution will be better if possible.

谢谢

推荐答案

我认为您的架构是正确的.您的问题归结为您的应用当前所处的应用状态之一.

I think your architecture is correct. Your issue is down to one of the App states your app is currently in.

如果您的应用程序被终止或从未被打开,则将执行 application:didFinishLaunchingWithOptions 中的上述代码,但是,如果您的应用程序处于后台或暂停状态,则以下方法将是叫.

If your app is terminated or has never been opened then the above code in application:didFinishLaunchingWithOptions will be exectued, however, if your app is in a background or suspended state then the below method will be called.

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo` 

因此,您还需要在应用委托中实现这一点.

Therefore you also need to implemented this in the app delegate.

希望能帮到你

这篇关于接收时执行 iOS 推送通知代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆