Flutter AppMetrica Push iOS
The AppMetrica Push SDK is a set of libraries for working with push notifications. After enabling the AppMetrica Push SDK, you can create and configure push notification campaigns, then monitor statistics in the AppMetrica web interface.
SDK features
- Receive and display Push notifications
- Receiving Silent Push notifications
- Processing payload from notifications
- image display in notifications
- support of deeplink action when opening a notification
- support for URL action, when you open a notification
Setup
Add this to your project's pubspec.yaml file:
dependencies:
firebase_core: <lastles>
firebase_analytics: <lastles>
appmetrica_plugin: <lastles>
appmetrica_push_ios: <lastles>
To get started with the SDK, you'll need:
- create a project in AppMetrica
- create a project in Firebase, add the iOS application and download the configuration file `GoogleService-Info.plist
- In the AppMetrica project settings, get the
API key (for use in the SDK)
- configure AppMetrica to work with APNs
(Optional) Enable push token updating: The APNS service can withdraw the push token of the device, for example, if the user did not launch the application for a long time. AppMetrica stores push tokens on the server and can not send a push notification to a device with an obsolete token. To automatically collect current push token go to the application settings in the AppMetrica interface and enable the Update tokens with a Silent Push notification option in the Push Notifications tab.
Connecting the AppMetrica Push SDK (an example can be found in examples/example_fcm)
Place the configuration file GoogleService-Info.plist
in <project>/ios/Runner/
via XCode.
Create a Notification Service Extension:
- In Xcode select
File → New → Target
. - In the iOS Extensions section, select
Notification Service Extension
from the list and pressNext
. - Enter the name of the extension in the
Product Name
field and clickFinish
.
Create a shared App Groups:
- In the Xcode project settings, go to the Capabilities tab.
- Switch on the App Groups option for the created extension and for the application. To switch between an extension and an app, go to the project settings panel and click or the drop-down element.
- In the App Groups section use the + button to create a group. You will need the group name during further configuration.
- Select the group you created for the app and for the extension.
In the created Notification Service Extension
change the code as follows:
import UserNotifications
import YandexMobileMetricaPush
class <YourNotificationServiceName>: UNNotificationServiceExtension {
private var contentHandler: ((UNNotificationContent) -> Void)?
private var bestAttemptContent: UNMutableNotificationContent?
private let syncQueue = DispatchQueue(label: "<YourNotificationServiceName>.syncQueue")
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
if bestAttemptContent != nil {
// Modify the notification content here...
YMPYandexMetricaPush.setExtensionAppGroup("<your.app.group.name>")
YMPYandexMetricaPush.handleDidReceive(request)
}
YMPYandexMetricaPush.downloadAttachments(for: request) { [weak self] attachments, error in
if let error = error {
print("Error: \(error)")
}
self?.completeWithBestAttempt(attachments: attachments)
}
}
override func serviceExtensionTimeWillExpire() {
completeWithBestAttempt(attachments: nil)
}
func completeWithBestAttempt(attachments: [UNNotificationAttachment]?) {
syncQueue.sync { [weak self] in
if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent {
if let attachments = attachments {
bestAttemptContent.attachments = attachments
}
contentHandler(bestAttemptContent)
self?.bestAttemptContent = nil
self?.contentHandler = nil
}
}
}
}
In <project>/ios/Runner/AppDelegate.swift
, add the following lines in application:didFinishLaunchingWithOptions
between GeneratedPluginRegistrant.register
and return super.application
:
...
YMPYandexMetricaPush.setExtensionAppGroup("<your.app.group.name>")
...
In the <project>/ios/Podfile
file, change and add:
platform :ios, '10.0'
...
target '<YourNotificationServiceName>' do
use_frameworks!
pod 'YandexMobileMetricaPush'
end
post_install do |installer|
installer.pods_project.targets.each do |target|
# In case of `Multiple commands produce .../XCFrameworkIntermediates/YandexMobileMetrica` problem
if target.name == 'YandexMobileMetrica-Static_Core'
target.remove_from_project
end
flutter_additional_ios_build_settings(target)
end
end
Initializing the AppMetrica Push SDK (an example can be found in examples/example_fcm)
await Firebase.initializeApp();
await FirebaseAnalytics.instance.setAnalyticsCollectionEnabled(true);
// AppMetrica.activate must be called before AppmetricaPush.activate
await AppMetrica.activate(AppMetricaConfig('<AppMetrica API key>'));
await AppmetricaPushIos.instance.activate();
await AppmetricaPushIos.instance.requestPermission(PermissionOptions(
alert: true,
badge: true,
sound: true,
));
Using the AppMetrica Push SDK (an example can be found in examples/example_fcm)
// Get a PUSH service token
await AppmetricaPushIos.instance.getTokens();
// Streaming PUSH service tokens. Comes when the token on the device changes
// Note that this is a stream broadcast
AppmetricaPushIos.instance.tokenStream.listen((Map<String, String?> data) => print('token: $data'));
// Stream silent push, as the data comes payload
// Note that this is a stream broadcast
AppmetricaPushIos.instance.onMessage
.listen((String data) => print('onMessage: $data'));
// Stream push, as the data comes payload
// Note that this is a stream broadcast
AppmetricaPushIos.instance.onMessageOpenedApp
.listen((String data) => print('onMessageOpenedApp: $data'));
Example of work
An example of how the SDK works is available at Example