首页 > 解决方案 > 关闭或滑出应用程序时未触发iOS Objective-C didReceiveRemoteNotification

问题描述

我已经在我的 iOS 应用中实现了 Firebase,并使用了纯数据负载。到目前为止,当应用程序打开或最小化时,我的自定义通知工作正常。但是当我刷应用程序时,我不再收到任何远程消息。当您滑动应用程序时,我不确定该应用程序是暂停还是未运行。

到目前为止,我做了这样的事情:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
   NSDictionary *userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
   CustomNotif *notif = [[CustomNotif alloc] init];

   [notif showNotification:userInfo];
}

CustomNotif 是我创建的用于处理通知 UI 的类。我已经将 aps 有效负载设置为:

优先级:“高”内容可用:1

这就是我收到远程消息时得到的:

{
  aps =     {
        "content-available" = 1;
    };
    body = "sample body";
    "gcm.message_id" = "0:1538531666855542%d0cc52a4d0cc52a4";
    iconURL = "http://www.shutterangle.com/wp-content/uploads/2012/04/bladerunner.jpg";
    title = "Urgent action needed!";
}

我只有在应用程序打开或最小化时才能获得该有效负载。当我滑动应用程序或关闭它时,有没有办法得到它?


我想添加更多信息。. 我尝试关闭应用程序并在xcode中查看设备日志,发现即使应用程序完全关闭,当有人发送远程通知时,应用程序仍然被触发;但是,我只是不知道如何获取数据。

这是每次有人发送通知时显示的日志:

Oct  4 14:21:57 apsd(CommonUtilities)[112] <Notice>: Created power assertion {identifier: APSCourier(tcpStream:dataReceived:)}
Oct  4 14:21:57 apsd[112] <Notice>: <private>: Outstanding data received: <private> (length 477) onInterface: NonCellular. Connected on 1 interfaces.
Oct  4 14:21:57 apsd[112] <Notice>: <private>: Stream processing: complete yes, invalid no, length parsed 473, parameters <private>
Oct  4 14:21:57 apsd(CommonUtilities)[112] <Notice>: Created power assertion {identifier: APSCourier(tcpStream:dataReceived:)}
Oct  4 14:21:57 apsd[112] <Notice>: copyTokenForDomain sandbox.push.apple.com (null)
Oct  4 14:21:57 apsd[112] <Notice>: <private> incoming message is for proxy slave that is *not* connected. baseToken <private>  destToken <private>  publicTokens <private>
Oct  4 14:21:57 apsd[112] <Notice>: <private> timestampForTopic? <private> token <private>
Oct  4 14:21:57 apsd[112] <Notice>: isAppSpecificTokenValid sandbox.push.apple.com <private>
Oct  4 14:21:57 apsd[112] <Notice>: isAppSpecificTokenValid? <private> found cached token YES
Oct  4 14:21:57 apsd[112] <Notice>: <private> hasPayload? <private> forTopic <private> tokens <private>
Oct  4 14:21:57 apsd[112] <Notice>: copyAppSpecificTokensWithDomain - sandbox.push.apple.com for topic <private> account <private>
Oct  4 14:21:57 apsd[112] <Notice>: <private> receivedPushWithTopic <private> token <private> payload <private> timestamp Thu Oct  4 14:21:57 2018
Oct  4 14:21:57 apsd[112] <Notice>: <private>: Received message for enabled topic '<private>' onInterface: NonCellular with payload '<private>' with priority 5 for device token: NO
Oct  4 14:21:57 apsd[112] <Notice>: <private> asked to store incoming message <private> with guid <private> environment <private>
Oct  4 14:21:57 apsd[112] <Notice>: <private>: Calling into AWD for PushReceived
Oct  4 14:21:57 apsd[112] <Notice>: submitted metric <private>
Oct  4 14:21:57 analyticsd[90] <Notice>: [<private>] no observers; dropped.
Oct  4 14:21:57 apsd[112] <Notice>: <private> deleting low-priority incoming messages for topic <private> environment development
Oct  4 14:21:57 apsd[112] <Notice>: APSMessageStore - APSIncomingMessageRecordDeleteMessagesForTopic <private> 1 environment development
Oct  4 14:21:57 apsd[112] <Notice>: APSMessageStore - New message record [<private>] has ID [6954].
Oct  4 14:21:57 apsd[112] <Notice>: <private>: AWD for PushReceived finished
Oct  4 14:21:57 apsd[112] <Notice>: <private>: Stream processing: complete yes, invalid no, length parsed 4, parameters <private>
Oct  4 14:21:57 apsd[112] <Notice>: <private>: Received keep-alive response 1 on interface NonCellular: <private>
Oct  4 14:21:57 apsd(CommonUtilities)[112] <Notice>: Releasing power assertion {identifier: APSCourier(tcpStream:dataReceived:)}
Oct  4 14:21:57 apsd(CommonUtilities)[112] <Notice>: Created power assertion {identifier: APSCourier(tcpStream:dataReceived:)}
Oct  4 14:21:57 apsd[112] <Notice>: <private>: Stream processing: complete no, invalid no, length parsed 0, parameters (null)
Oct  4 14:21:57 apsd[112] <Notice>: <private> calling completion block for incoming message <private>
Oct  4 14:21:57 apsd[112] <Notice>: <private>: Sending acknowledgement message with response 0 and messageId <private> (1505639937)
Oct  4 14:21:57 apsd[112] <Notice>: <private> Noting push - using last lq 0 and rat (null)  (instead of -2, kCTRegistrationRadioAccessTechnologyUTRAN)
Oct  4 14:21:57 apsd[112] <Notice>: <private>:APSNetworkMonitor decaying cost (1498 - 4) = 1494 for 14.404547 seconds
Oct  4 14:21:57 apsd[112] <Notice>: <private>:APSNetworkMonitor addCost: 166 - _currentCost is now 1500
Oct  4 14:21:57 apsd[112] <Notice>: <private> _notifyForIncomingMessage <private> with guid <private>
Oct  4 14:21:57 apsd[112] <Notice>: Dispatching low priority message: <xpc object>
Oct  4 14:21:57 SpringBoard(ApplePushService)[55] <Notice>: <private> Delivering message from apsd: <private> 1505639937 <private>
Oct  4 14:21:57 SpringBoard(ApplePushService)[55] <Notice>: <private> making delegate (<private>) calls to deliver message 1505639937 <private> for topic <private>
Oct  4 14:21:57 SpringBoard(ApplePushService)[55] <Notice>: <private> calling <private> connection:didReceiveIncomingMessage:
Oct  4 14:21:57 SpringBoard(ApplePushService)[55] <Notice>: <private> returned from <private> connection:didReceiveIncomingMessage:
Oct  4 14:21:57 SpringBoard(ApplePushService)[55] <Notice>: <private> responding with an ack for message with guid <private>
Oct  4 14:21:57 SpringBoard(UserNotificationsServer)[55] <Notice>: Received incoming message on topic com.rometic.trulyrussiandev at priority 1
Oct  4 14:21:57 apsd[112] <Notice>: Looking up connection on peer: 5171ff0   found <private>
Oct  4 14:21:57 apsd[112] <Notice>: <private> informed that <private> acknowledges incoming message with guid <private>
Oct  4 14:21:57 apsd[112] <Notice>: <private> Removing incoming message with guid <private>
Oct  4 14:21:57 apsd[112] <Notice>: <private> _schedulePendingWorkUpdate
Oct  4 14:21:57 apsd[112] <Notice>: APSMessageStore - APSIncomingMessageRecordDeleteMessageForGUID <private>
Oct  4 14:21:57 SpringBoard(UserNotificationsServer)[55] <Notice>: [com.rometic.trulyrussiandev] Received remote notification request 135F-D297 [ hasAlertContent: 0, hasSound: 0 hasBadge: 0 hasContentAvailable: 1 hasMutableContent: 0 ]
Oct  4 14:21:57 SpringBoard(UserNotificationsServer)[55] <Notice>: [com.rometic.trulyrussiandev] Deliver push notification request 135F-D297
Oct  4 14:21:57 SpringBoard(UserNotificationsServer)[55] <Notice>: [com.rometic.trulyrussiandev] Passing content-available push to Duet
Oct  4 14:21:57 SpringBoard(DuetActivityScheduler)[55] <Notice>: SUBMITTING: <private>
Oct  4 14:21:57 SpringBoard(UserNotificationsServer)[55] <Error>: Ignoring notification with no alert, sound or badge (com.rometic.trulyrussiandev): 135F-D297
Oct  4 14:21:57 SpringBoard(UserNotificationsServer)[55] <Notice>: [com.rometic.trulyrussiandev] Not saving push notification 135F-D297 to store [ error=Error Domain=UNErrorDomain Code=1401 "Notification has no user-facing content" UserInfo={NSLocalizedDescription=Notification has no user-facing content} ]
Oct  4 14:21:57 dasd(DuetActivitySchedulerDaemon)[153] <Notice>: Submitted Activity: com.apple.pushLaunch.com.rometic.trulyrussiandev:DD5321 <private>
Oct  4 14:21:57 apsd[112] <Notice>: Created metric container: 0x130004 succeeded? YES
Oct  4 14:21:57 dasd(DuetActivitySchedulerDaemon)[153] <Notice>: Daemon Canceling Activities: {(
    com.apple.pushLaunch.com.rometic.trulyrussiandev:DD5321
)}
Oct  4 14:21:57 dasd(DuetActivitySchedulerDaemon)[153] <Notice>: CANCELED: com.apple.pushLaunch.com.rometic.trulyrussiandev:DD5321 <private>!
Oct  4 14:21:57 dasd(DuetActivitySchedulerDaemon)[153] <Notice>: Removing a launch request for application <private> by activity <private>
Oct  4 14:21:57 apsd[112] <Notice>: Submitted metric: 0x130004 succeeded? YES
Oct  4 14:21:57 apsd(WirelessDiagnostics)[112] <Notice>: client.trigger:#N Random sample for 0x130004 is skip
Oct  4 14:21:57 SpringBoard(DuetActivityScheduler)[55] <Notice>: cancelActivities: 1 activities were not found: <private>
Oct  4 14:21:57 analyticsd[90] <Notice>: [<private>] no observers; dropped.
Oct  4 14:21:57 rtcreportingd[224] <Notice>: releaseXPCTransactions: XPC transaction ref count is 0, decremented by 1
Oct  4 14:21:57 rtcreportingd[224] <Notice>: releaseXPCTransactions: XPC transaction ended
Oct  4 14:21:57 symptomsd(SymptomEvaluator)[122] <Notice>: L2 Metrics on ifname en0: rssi: -56 (txFrames/txReTx/txFail) 4/2/0 -> (was/is) 0/0
Oct  4 14:21:57 sharingd(WirelessProximity)[61] <Notice>: Nearby stop advertising of type: 16
Oct  4 14:21:57 sharingd(WirelessProximity)[61] <Notice>: Nearby start advertising with data: Advertising request of type 16, priority 1, UseFG 432 (270.00 ms), data <0b10dfe0 72>, connectable 1, options {
} priority 0 mode 432 options (null)
Oct  4 14:21:57 wirelessproxd[59] <Notice>: Stop advertising for process sharingd (61) of type 16
Oct  4 14:21:57 wirelessproxd[59] <Notice>: Nothing left to advertise, stopping
Oct  4 14:21:57 wirelessproxd[59] <Notice>: Start advertising for process sharingd (61) of type 16 with advertising interval 432 (270.00 ms)
Oct  4 14:21:57 wirelessproxd[59] <Notice>: Adding data of type: 16, advData: <0b10dfe0 72> advInterval: 432
Oct  4 14:21:57 wirelessproxd[59] <Notice>: Current advertisement packet: <10050b10 dfe072>
Oct  4 14:21:57 bluetoothd[79] <Notice>: Received XPC message "CBMsgIdStopAdvertising" from session "com.apple.wirelessproxd-peripheral-59-2"
Oct  4 14:21:57 bluetoothd[79] <Notice>: Received 'stop advertising' request from session "com.apple.wirelessproxd-peripheral-59-2"
Oct  4 14:21:57 bluetoothd[79] <Notice>: Sending XPC message "CBMsgIdAdvertisingStopped" to session "com.apple.wirelessproxd-peripheral-59-2"
Oct  4 14:21:57 bluetoothd[79] <Notice>: Received XPC message "CBMsgIdStopAdvertising" from session "com.apple.wirelessproxd-peripheral-59-3"
Oct  4 14:21:57 bluetoothd[79] <Notice>: Sending XPC message "CBMsgIdAdvertisingStopped" to session "com.apple.wirelessproxd-peripheral-59-3"
Oct  4 14:21:57 bluetoothd[79] <Notice>: Sending 'advertising status changed' event to local device listeners
Oct  4 14:21:57 wirelessproxd[59] <Notice>: AdvertisingRulesiOS: current advertisers: <NSMutableIndexSet: 0x119e0c490>[number of indexes: 1 (in 1 ranges), indexes: (16)]
Oct  4 14:21:57 wirelessproxd[59] <Notice>: advertisingRulesiOS - advertising packets: (
        {
        kCBAdvDataAppleMfgData = <0aff4c00 10050b10 dfe072>;
        kCBAdvDataIsConnectable = 1;
        kCBAdvOptionAdvertisingInterval = 432;
        kCBMsgArgIsPrivilegedDaemon = 1;
    }
)
Oct  4 14:21:57 wirelessproxd[59] <Notice>: Requesting to start advertising for clients 16  with (
        {
        kCBAdvDataAppleMfgData = <0aff4c00 10050b10 dfe072>;
        kCBAdvDataIsConnectable = 1;
        kCBAdvOptionAdvertisingInterval = 432;
        kCBMsgArgIsPrivilegedDaemon = 1;
    }
)

日志显示远程消息正在后台处理,但我不知道为什么它不能点击通知。我注意到这被称为"connection:didReceiveIncomingMessage"

请帮忙。

标签: iosobjective-cfirebasefirebase-cloud-messaging

解决方案


您应该使用以下方法处理推送通知

- (void)application:(UIApplication *)application 
didReceiveRemoteNotification:(NSDictionary *)userInfo 
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler;

此方法用于处理传入的通知。当您的应用程序在前台或后台运行时调用它。如果您启用了远程通知后台模式,系统将启动您的应用程序并将其置于后台状态以处理通知(注意 - 如果用户强制退出应用程序,它将不会重新启动应用程序)。


推荐阅读