एक बार जब आपका क्लाइंट ऐप किसी डिवाइस पर इंस्टॉल हो जाता है, तो वह FCM APNs इंटरफ़ेस के माध्यम से संदेश प्राप्त कर सकता है। आप नोटिफिकेशन कंपोजर , या अपने एप्लिकेशन सर्वर पर बनाए गए संदेशों के साथ उपयोगकर्ता सेगमेंट को तुरंत सूचनाएं भेजना शुरू कर सकते हैं।
अलर्ट नोटिफिकेशन हैंडल करें
FCM APN के माध्यम से Apple ऐप्स को लक्षित करने वाले सभी संदेशों को डिलीवर करता है। UNUserNotificationCenter के माध्यम से APN सूचनाएँ प्राप्त करने के बारे में अधिक जानने के लिए, Apple के दस्तावेज़ीकरण पर सूचनाएँ और सूचना-संबंधित क्रियाएँ देखें।
आपको FCM से प्रदर्शन सूचनाएं प्राप्त करने के लिए UNUserNotificationCenter प्रतिनिधि सेट करना होगा और उपयुक्त प्रतिनिधि विधियों को लागू करना होगा।
तीव्र
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
पैरामीटर सेट करें। उस मान का उपयोग करें जिसका उपयोग आप APNs पेलोड में category
कुंजी के लिए करेंगे। उपयोग किए जाने से पहले कस्टम क्रियाओं को पंजीकृत होना चाहिए। अधिक जानकारी के लिए, Apple की स्थानीय और दूरस्थ सूचना प्रोग्रामिंग मार्गदर्शिका देखें।
अपने ऐप पर संदेश वितरण की जानकारी के लिए, FCM रिपोर्टिंग डैशबोर्ड देखें, जो Android ऐप्स के लिए "इंप्रेशन" (उपयोगकर्ताओं द्वारा देखी गई सूचनाएं) के डेटा के साथ-साथ Apple और Android उपकरणों पर भेजे गए और खोले गए संदेशों की संख्या को रिकॉर्ड करता है।
साइलेंट पुश नोटिफिकेशन हैंडल करें
content_available
कुंजी (एपीएन की 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 प्लेटफ़ॉर्म पृष्ठभूमि सूचनाओं के वितरण की गारंटी नहीं देते हैं। उन स्थितियों के बारे में जानने के लिए जो पृष्ठभूमि सूचनाओं को विफल कर सकती हैं, Apple के डॉक्स ऑन पुशिंग बैकग्राउंड अपडेट्स टू योर ऐप देखें।
सूचना संदेश पेलोड की व्याख्या करना
अधिसूचना संदेशों का पेलोड कुंजियों और मूल्यों का एक शब्दकोश है। एपीएन के माध्यम से भेजे गए अधिसूचना संदेश नीचे दिए गए एपीएन पेलोड प्रारूप का पालन करते हैं:
{ "aps" : { "alert" : { "body" : "great match!", "title" : "Portugal vs. Denmark", }, "badge" : 1, }, "customKey" : "customValue" }
विधि स्विज़लिंग अक्षम वाले संदेशों को संभालें
डिफ़ॉल्ट रूप से, यदि आप अपने ऐप के ऐप प्रतिनिधि वर्ग को UNUserNotificationCenter
और Messaging
प्रतिनिधि गुणों को असाइन करते हैं, तो FCM आपके FCM टोकन को डिवाइस के APN टोकन के साथ स्वचालित रूप से संबद्ध करने के लिए आपके ऐप प्रतिनिधि वर्ग को स्विज़ल कर देगा और सूचना-प्राप्त ईवेंट को एनालिटिक्स को पास कर देगा। यदि आप स्विजलिंग विधि को स्पष्ट रूप से अक्षम करते हैं, यदि आप एक SwiftUI ऐप बना रहे हैं, या यदि आप किसी प्रतिनिधि के लिए एक अलग वर्ग का उपयोग करते हैं, तो आपको इन दोनों कार्यों को मैन्युअल रूप से करने की आवश्यकता होगी।
डिवाइस APNs टोकन के साथ 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); }