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

查看:167
本文介绍了接收时执行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.另外,我会在收到通知时在客户端计算徽章编号.因此,如果可能的话,代码执行解决方案会更好.

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天全站免登陆