appmetrica_push 1.0.0 copy "appmetrica_push: ^1.0.0" to clipboard
appmetrica_push: ^1.0.0 copied to clipboard

The AppMetrica Push SDK is a set of libraries for working with push notifications.

Flutter AppMetrica Push

Read this in Russian

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: <lastles>

To get started with the SDK, you'll need: #

(Optional) Enable push token updating: The FCM 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) #

Android #

In the file <project>/android/app/build.gradle put minSdkVersion 19

Using the Google Services Plugin Place the configuration file google-services.json in the module directory of the project(").

In the file " add:


buildscript {
  dependencies {
    ...
    classpath 'com.google.gms:google-services:4.3.13'
  }
}

In the file " add:


...
apply plugin: 'com.google.gms.google-services'
Without using the plugin Make changes to the application element of the AndroidManifest.xml file:

<meta-data android:name="ymp_firebase_default_app_id" android:value="APP_ID"/>
<meta-data android:name="ymp_gcm_default_sender_id" android:value="number:SENDER_ID"/>
<meta-data android:name="ymp_firebase_default_api_key" android:value="API_KEY"/>
<meta-data android:name="ymp_firebase_default_project_id" android:value="PROJECT_ID"/>

APP_ID — ID of the app in Firebase. You can find it in the Firebase console: go to the Project settings. In the Your application section copy the value of the application ID field.

SENDER_ID — The unique ID of the sender in Firebase. You can find it in the Firebase console: go to Project settings → Cloud Messaging and copy the value of the Sender ID field.

API_KEY — App key in Firebase. You can find it in the current_key field of the google-services.json file. You can download the file in the Firebase console.

PROJECT_ID — App ID in Firebase. You can find it in the project_id field of the google-services.json file. You can download the file in the Firebase console.

iOS #

Place the configuration file GoogleService-Info.plist in <project>/ios/Runner/ via XCode.

Create a Notification Service Extension:

  1. In Xcode select File → New → Target.
  2. In the iOS Extensions section, select Notification Service Extension from the list and press Next.
  3. Enter the name of the extension in the Product Name field and click Finish.

Create a shared App Groups:

  1. In the Xcode project settings, go to the Capabilities tab.
  2. 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.
  3. In the App Groups section use the + button to create a group. You will need the group name during further configuration.
  4. 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 AppmetricaPush.instance.activate();
await AppmetricaPush.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 AppmetricaPush.instance.getTokens();

// Streaming PUSH service tokens. Comes when the token on the device changes
// Note that this is a stream broadcast
AppmetricaPush.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
AppmetricaPush.instance.onMessage
      .listen((String data) => print('onMessage: $data'));

// Stream push, as the data comes payload
// Note that this is a stream broadcast
AppmetricaPush.instance.onMessageOpenedApp
      .listen((String data) => print('onMessageOpenedApp: $data'));

Example of work #

An example of how the SDK works is available at Example