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

PlatformiOS

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

Flutter AppMetrica Push iOS

Read this in Russian

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 documentation.

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

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

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

3
likes
130
pub points
4%
popularity

Publisher

verified publishermadbrains.ru

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

Homepage
Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (LICENSE)

Dependencies

appmetrica_push_platform_interface, flutter

More

Packages that depend on appmetrica_push_ios