एक बार जब आपका क्लाइंट ऐप डिवाइस पर इंस्टॉल हो जाता है, तो यह एफसीएम एपीएन इंटरफ़ेस के माध्यम से संदेश प्राप्त कर सकता है। आप नोटिफिकेशन कंपोजर , या अपने एप्लिकेशन सर्वर पर निर्मित संदेशों के साथ तुरंत उपयोगकर्ता खंडों को सूचनाएं भेजना शुरू कर सकते हैं।
चेतावनी सूचनाएं संभालें
FCM APNs के माध्यम से Apple ऐप्स को लक्षित करने वाले सभी संदेश वितरित करता है। UNUserNotificationCenter के माध्यम से APN सूचनाएं प्राप्त करने के बारे में अधिक जानने के लिए, सूचनाओं और अधिसूचना-संबंधित कार्रवाइयों को संभालने पर Apple का दस्तावेज़ देखें।
आपको UNUserNotificationCenter प्रतिनिधि को सेट करना होगा और FCM से प्रदर्शन सूचनाएं प्राप्त करने के लिए उचित प्रतिनिधि तरीकों को लागू करना होगा।
तीव्र
extension AppDelegate: UNUserNotificationCenterDelegate { // Receive displayed notifications for iOS 10 devices. func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification) async -> UNNotificationPresentationOptions { let userInfo = notification.request.content.userInfo // With swizzling disabled you must let Messaging know about the message, for Analytics // Messaging.messaging().appDidReceiveMessage(userInfo) // ... // Print full message. print(userInfo) // Change this to your preferred presentation option return [[.alert, .sound]] } func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse) async { let userInfo = response.notification.request.content.userInfo // ... // With swizzling disabled you must let Messaging know about the message, for Analytics // Messaging.messaging().appDidReceiveMessage(userInfo) // Print full message. print(userInfo) } }
उद्देश्य सी
// Receive displayed notifications for iOS 10 devices. // Handle incoming notification messages while app is in the foreground. - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler { NSDictionary *userInfo = notification.request.content.userInfo; // With swizzling disabled you must let Messaging know about the message, for Analytics // [[FIRMessaging messaging] appDidReceiveMessage:userInfo]; // ... // Print full message. NSLog(@"%@", userInfo); // Change this to your preferred presentation option completionHandler(UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionAlert); } // Handle notification messages after display notification is tapped by the user. - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler { NSDictionary *userInfo = response.notification.request.content.userInfo; if (userInfo[kGCMMessageIDKey]) { NSLog(@"Message ID: %@", userInfo[kGCMMessageIDKey]); } // With swizzling disabled you must let Messaging know about the message, for Analytics // [[FIRMessaging messaging] appDidReceiveMessage:userInfo]; // Print full message. NSLog(@"%@", userInfo); completionHandler(); }
यदि आप अपनी सूचनाओं में कस्टम कार्रवाइयां जोड़ना चाहते हैं, तो अधिसूचना पेलोड में click_action
पैरामीटर सेट करें। उस मान का उपयोग करें जिसे आप एपीएन पेलोड में category
कुंजी के लिए उपयोग करेंगे। कस्टम कार्रवाइयों का उपयोग करने से पहले उन्हें पंजीकृत किया जाना चाहिए। अधिक जानकारी के लिए, Apple की लोकल और रिमोट नोटिफिकेशन प्रोग्रामिंग गाइड देखें।
अपने ऐप पर संदेश वितरण की जानकारी के लिए, एफसीएम रिपोर्टिंग डैशबोर्ड देखें, जो ऐप्पल और एंड्रॉइड डिवाइस पर भेजे गए और खोले गए संदेशों की संख्या को रिकॉर्ड करता है, साथ ही एंड्रॉइड ऐप के लिए "इंप्रेशन" (उपयोगकर्ताओं द्वारा देखी गई सूचनाएं) के डेटा को भी रिकॉर्ड करता है।
मौन पुश सूचनाएँ संभालें
content_available
कुंजी (APNs के content-available
के समतुल्य) के साथ संदेश भेजते समय, संदेशों को मूक सूचनाओं के रूप में वितरित किया जाएगा, जो पृष्ठभूमि डेटा रीफ्रेश जैसे कार्यों के लिए पृष्ठभूमि में आपके ऐप को सक्रिय करेगा। अग्रभूमि सूचनाओं के विपरीत, इन सूचनाओं को application(_:didReceiveRemoteNotification:fetchCompletionHandler:)
विधि।
application(_:didReceiveRemoteNotification:fetchCompletionHandler:)
जैसा कि दिखाया गया है:
तीव्र
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) async -> UIBackgroundFetchResult { // If you are receiving a notification message while your app is in the background, // this callback will not be fired till the user taps on the notification launching the application. // TODO: Handle data of notification // With swizzling disabled you must let Messaging know about the message, for Analytics // Messaging.messaging().appDidReceiveMessage(userInfo) // Print message ID. if let messageID = userInfo[gcmMessageIDKey] { print("Message ID: \(messageID)") } // Print full message. print(userInfo) return UIBackgroundFetchResult.newData }
उद्देश्य सी
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { // If you are receiving a notification message while your app is in the background, // this callback will not be fired till the user taps on the notification launching the application. // TODO: Handle data of notification // With swizzling disabled you must let Messaging know about the message, for Analytics // [[FIRMessaging messaging] appDidReceiveMessage:userInfo]; // ... // Print full message. NSLog(@"%@", userInfo); completionHandler(UIBackgroundFetchResultNewData); }
Apple प्लेटफ़ॉर्म पृष्ठभूमि सूचनाओं की डिलीवरी की गारंटी नहीं देता है। उन स्थितियों के बारे में जानने के लिए जिनके कारण पृष्ठभूमि सूचनाएं विफल हो सकती हैं, अपने ऐप में पृष्ठभूमि अपडेट पुश करने पर ऐप्पल के दस्तावेज़ देखें।
अधिसूचना संदेश पेलोड की व्याख्या करना
अधिसूचना संदेशों का पेलोड कुंजियों और मूल्यों का एक शब्दकोश है। एपीएन के माध्यम से भेजे गए अधिसूचना संदेश नीचे दिए गए एपीएन पेलोड प्रारूप का पालन करते हैं:
{ "aps" : { "alert" : { "body" : "great match!", "title" : "Portugal vs. Denmark", }, "badge" : 1, }, "customKey" : "customValue" }
मेथड स्विज़लिंग अक्षम के साथ संदेशों को संभालें
डिफ़ॉल्ट रूप से, यदि आप अपने ऐप के ऐप प्रतिनिधि वर्ग को UNUserNotificationCenter
और Messaging
प्रतिनिधि गुणों को निर्दिष्ट करते हैं, तो FCM आपके FCM टोकन को डिवाइस के APNs टोकन के साथ स्वचालित रूप से संबद्ध करने और अधिसूचना-प्राप्त घटनाओं को एनालिटिक्स को पास करने के लिए आपके ऐप प्रतिनिधि वर्ग को घुमाएगा। यदि आप मेथड स्विज़लिंग को स्पष्ट रूप से अक्षम करते हैं, यदि आप एक स्विफ्टयूआई ऐप बना रहे हैं, या यदि आप किसी भी प्रतिनिधि के लिए एक अलग क्लास का उपयोग करते हैं, तो आपको इन दोनों कार्यों को मैन्युअल रूप से करने की आवश्यकता होगी।
एफसीएम टोकन को डिवाइस एपीएन टोकन के साथ जोड़ने के लिए, एपीएन टोकन को apnsToken
प्रॉपर्टी के माध्यम से अपने ऐप प्रतिनिधि के टोकन रीफ्रेश हैंडलर में Messaging
क्लास में पास करें।
तीव्र
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { Messaging.messaging().apnsToken = deviceToken; }
उद्देश्य सी
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { [FIRMessaging messaging].APNSToken = deviceToken; }
एनालिटिक्स को अधिसूचना प्राप्त जानकारी भेजने के लिए, appDidReceiveMessage(_:)
विधि का उपयोग करें।
तीव्र
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { let userInfo = notification.request.content.userInfo Messaging.messaging().appDidReceiveMessage(userInfo) // Change this to your preferred presentation option completionHandler([[.alert, .sound]]) } func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { let userInfo = response.notification.request.content.userInfo Messaging.messaging().appDidReceiveMessage(userInfo) completionHandler() } func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { Messaging.messaging().appDidReceiveMessage(userInfo) completionHandler(.noData) }
उद्देश्य सी
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler { NSDictionary *userInfo = notification.request.content.userInfo; [[FIRMessaging messaging] appDidReceiveMessage:userInfo]; // Change this to your preferred presentation option completionHandler(UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionAlert); } - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler { NSDictionary *userInfo = response.notification.request.content.userInfo; [[FIRMessaging messaging] appDidReceiveMessage:userInfo]; completionHandler(); } - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler { [[FIRMessaging messaging] appDidReceiveMessage:userInfo]; completionHandler(UIBackgroundFetchResultNoData); }