react-native-appmetrica/README.md

190 lines
4.8 KiB
Markdown
Raw Normal View History

2021-01-21 07:29:24 +03:00
# react-native-appmetrica-next
2022-05-19 08:07:44 +03:00
WARNING!!!
ver 2.0 RN68 >=
ver 1.0.17 RN67 <=
2020-06-08 22:14:06 +03:00
React Native bridge to the [AppMetrica](https://appmetrica.yandex.com/) on both iOS and Android.
2021-01-21 07:29:24 +03:00
react-native-push-next library functionality is expanded [react-native-appmetrica](https://github.com/yandexmobile/react-native-appmetrica)
2020-06-08 22:14:06 +03:00
## Installation
2021-01-21 07:29:24 +03:00
`npm install react-native-appmetrica-next --save`
or
`yearn add react-native-appmetrica-next`
2021-02-17 15:32:04 +03:00
## Usage
```js
import AppMetrica from "react-native-appmetrica-next";
// Starts the statistics collection process.
AppMetrica.activate({
apiKey: "...KEY...",
sessionTimeout: 120,
firstActivationAsUpdate: true,
});
// Sends a custom event message and additional parameters (optional).
AppMetrica.reportEvent("My event");
AppMetrica.reportEvent("My event", { foo: "bar" });
// Send a custom error event.
AppMetrica.reportError("My error");
// reportUserProfile
AppMetrica.activate({
apiKey: "...KEY...",
sessionTimeout: 120,
firstActivationAsUpdate: true,
});
RNAppMetrica.setUserProfileID("id");
RNAppMetrica.reportUserProfile({
name: "Andrey Bondarenko",
floor: "male",
age: 34,
isNotification: true,
});
```
# SETTING PUSH SDK
## NEXT for Android
2021-02-17 15:01:55 +03:00
2021-02-17 15:06:22 +03:00
## create file FirebaseMessagingMasterService.java in you project
2021-02-17 15:01:55 +03:00
2021-02-17 15:06:22 +03:00
```js
2021-02-17 15:01:55 +03:00
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import com.yandex.metrica.push.firebase.MetricaMessagingService;
public class FirebaseMessagingMasterService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage message) {
super.onMessageReceived(message);
// AppMetrica automatically recognizes its messages and processes them only.
new MetricaMessagingService().processPush(this, message);
// Implement the logic for sending messages to other SDKs.
}
}
2021-02-17 15:06:22 +03:00
```
## Your files to Android manifest
2021-02-17 15:01:55 +03:00
2021-02-17 15:06:22 +03:00
```js
<application>
...
<service
android:name=".FirebaseMessagingMasterService"
android:enabled="true"
android:exported="false"
>
<intent-filter android:priority="100">
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service
android:name="com.yandex.metrica.push.firebase.MetricaMessagingService"
tools:node="remove"
/>
...
</application>
```
2021-02-17 15:01:55 +03:00
2021-02-17 15:32:04 +03:00
## Silent Push Notifications for Android
## create file BroadcastReceiver in you project
```js
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import com.yandex.metrica.push.YandexMetricaPush;
public class SilentPushReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
String payload = intent.getStringExtra(YandexMetricaPush.EXTRA_PAYLOAD);
throw new UnsupportedOperationException("Not yet implemented");
}
}
```
## Your files to Android manifest
```js
<application>
...
<receiver android:name=".SilentPushReceiver">
<intent-filter>
<!-- Receive silent push notifications. -->
<action android:name="${applicationId}.action.ymp.SILENT_PUSH_RECEIVE"/>
</intent-filter>
</receiver>
```
2021-02-17 15:06:22 +03:00
# NEXT for iOS
2021-02-17 15:01:55 +03:00
2021-02-17 15:32:04 +03:00
### file AppDelegate.m add
2020-06-08 22:14:06 +03:00
```js
2021-02-17 15:32:04 +03:00
// Add import
#import <YandexMobileMetricaPush/YMPYandexMetricaPush.h>
2020-06-08 22:14:06 +03:00
2021-02-17 15:32:04 +03:00
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
...
...
// Enable in-app push notifications handling in iOS 10
if ([UNUserNotificationCenter class] != nil) {
id<YMPUserNotificationCenterDelegate> delegate = [YMPYandexMetricaPush userNotificationCenterDelegate];
delegate.nextDelegate = [UNUserNotificationCenter currentNotificationCenter].delegate;
[UNUserNotificationCenter currentNotificationCenter].delegate = delegate;
}
[YMPYandexMetricaPush handleApplicationDidFinishLaunchingWithOptions:launchOptions];
...
...
}
2020-06-08 22:14:06 +03:00
2021-02-17 15:32:04 +03:00
// and ADD
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
{
[YMPYandexMetricaPush handleRemoteNotification:userInfo];
}
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
[YMPYandexMetricaPush handleRemoteNotification:userInfo];
}
2020-06-08 22:14:06 +03:00
2021-02-17 15:32:04 +03:00
...
...
@end
```
2021-01-21 14:00:35 +03:00
2021-02-17 15:32:04 +03:00
## Usage (FIREBASE CLOUD MESSAGE)
```js
import AppMetrica from "react-native-appmetrica-next";
2021-01-21 14:00:35 +03:00
// init Push SDK example for iOS
checkPermission = async () => {
const authorizationStatus = await messaging().requestPermission();
if (authorizationStatus === messaging.AuthorizationStatus.AUTHORIZED) {
const deviceToken = await messaging().getToken();
2021-02-17 15:32:04 +03:00
RNAppMetrica.initPush();
2021-01-21 14:00:35 +03:00
2020-06-08 22:14:06 +03:00
```