#SDK Related

#1 Add push SDK and related configuration

#1.1 Add Maven library address

In the top-level build.gradle file named after the project name, add the google service dependency and the maven library address as follows:

buildscript {
    ......

    dependencies {
        classpath 'com.android.tools.build:gradle:3.4.1'
        classpath 'com.google.gms:google-services:4.2.0'
    }
}

allprojects {
    repositories {
        maven{ url 'https://mvn.shalltry.com/repository/maven-public'}
        ......
    }
}

#1.2 Configuration dependencies

Refer to the push SDK dependency library in app / build.gradle file as follows:

apply plugin: 'com.google.gms.google-services'
    defaultConfig {
        ......
        manifestPlaceholders = [
                "tpush_appid" : "xxxxxx",
                "tpush_appkey": "xxxxxx"
        ]
    }
......
dependencies {
   ......
   api("com.transsion.tpush:core:1.1.3.04")
   api 'com.google.firebase:firebase-messaging:18.0.0'
}
apply plugin: 'com.google.gms.google-services'

Note: tpush_appid and tpush_appkey are viewed in the push background app details

#2 Integration code

#2.1 Initialize the SDK

Initialize the SDK in the Application of the application, as shown below

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        PushManager.getInstance().init(getApplicationContext());
        ......
    }
}

#2.2 Notification style

Current push notification support: system default notification and custom notification style.
Default notification style support: title, content, expanded text and expanded image
Custom notification style support: icon, title, content, button icon, and expanded text and expanded image

Area 5 shown below, can be text or picture

Custom notification initialization

PushManager.getInstance().addCustomNotification(new PushNotification.Builder()
      .setType(PushConstants.NOTIFICATION_DEFAULT_TYPE) // Set notification style type
     .setSmallIcon(R.drawable.test_notification_small_icon) // Set notification icon, required
     .setShowDefaultLargeIcon(true) // Set to display a large picture of the application icon (not displayed by default)
     .build());

// Can add multiple
PushManager.getInstance().addCustomNotification(new PushNotification.Builder()
     .setStyleId(2) // Set Layout Style Id
     .setChannelId("ch2")
     .setType(PushConstants.NOTIFICATION_CUSTOM_TYPE) // Set notification style type
     .setSmallIcon(R.drawable.test_notification_small_icon) // Set notification icon, required
     .setLayoutId(R.layout.custom_style_3) // Set custom style notification layout
     .setIconId(R.id.notification_icon) // Set custom notification icon
     .setTitleId(R.id.notification_title) // Set custom notification title
     .setContentId(R.id.notification_content) // Set custom notification content
     .setBtnId(R.id.notification_btn) // Set custom notification button content
     .setContentBigId(R.id.notification_content_ex)
     .setImgBigId(R.id.notification_img_ex)
     .build());

Reference style: custom_style_3.xml
Note: Only views supported by RemoteViews can be used in custom layouts.
setStyleId represents the style Id set by the access application. When sending a message in the push background, you can fill in the corresponding Id, and you can use a custom layout.
setChannelId means set channel Id
addCustomNotification means add a custom notification, you can add multiple

Notification Icon
This icon will be displayed at the top of the notification bar as a notification icon, as shown below

The recommended size of the notification icon is as follows:

dpiIcon size
ldpi18*18
mdpi24*24
hdpi36*36
xhdpi48*48
xxhdpi72*72
xxxhdpi96*96

#2.3 Data transparent transmission and initialization completed

Data transparent transmission: You can customize the data format on the push platform and receive the data in the form of broadcast callback in the App.
Broadcast is registered in Application as follows:

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        // push initialization
        PushManager.getInstance().init(getApplicationContext());
        // Pass-through registration
        BroadcastReceiver broadcastReceiver = new PushBroadcastReceiver();
        PushManager.getInstance().registerReceiver(getApplicationContext(), broadcastReceiver);
    }
}

PushBroadcastReceiver inherits IPushBroadcastReceiver, the specific implementation is as follows

public class PushBroadcastReceiver extends IPushBroadcastReceiver {
    @Override
    public void onMessageReceive(Context context, long messageId, String transData) {
        Log.d("PUSH_", context.getPackageName() + " receive a transmit message, " + transData);
        // Show athena points (added where appropriate)
        // PushManager.getInstance().trackShow(messageId);
        // Click athena points (added where appropriate)
        // PushManager.getInstance().trackClick(messageId);
        // Conversion athena points (added where appropriate)
        // PushManager.getInstance().trackConversion(messageId);
    }

    @Override
    public void onSdkInitSuccess(Context context, String clientId, String token) {
        Log.d("PUSH_", context.getPackageName() + " init complete clientId:" + clientId + ", token:" + token);
    }
}

Note: The two methods trackShow and trackClick are called in the callback, which are used to push the statistics of the embedded point of the impression and click. After the call, you can view the number of impressions / impressions and clicks / clicks of the transparent data.
onSdkInitSuccess callback for initialization completion
onMessageReceive callback for transparent message

#PushManager.getInstance().trackShow(messageId);

The buried point interface is displayed for notification, that is, when a push is sent as a transparent message and used to display the notification, this interface is called after the notify method of the NotificationManager is called, as in the following example:

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
......
notificationManager.notify(id, notification);
PushManager.getInstance().trackShow(messageId);
#PushManager.getInstance().trackClick(messageId);

Click the buried point interface for notification, that is, when a push is sent as a transparent message and is used to display the notification, this interface is called after the user clicks the notification, as shown in the following example:

Notification.Builder builder;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            builder = new Notification.Builder(context, channelId);
        } else {
            builder = new Notification.Builder(context);
        }
        builder.setAutoCancel(true)
                .setContentIntent(pendingIntent)

pendingIntent is the action after the user clicks the notification, including the component broadcast, activity, or service. It is necessary to call the click buried point interface when starting these components.

#2.4 clientId and token acquisition

After 1.1.2.xx, the clientId and Token acquisition interfaces are provided as follows:

PushManager.getInstance().getClientId(new IClientIdListener() {
    @Override
    public void onFail(String msg) {
        Log.d("PUSH_", "getClientId fail:" + msg);
    }

    @Override
    public void onSuccess(String clientId) {
        Log.d("PUSH_", "getClientId success:" + clientId);
    }
});

String token = PushManager.getInstance().getToken(context);

#2.5 Topic subscription

Subscribe subscribeToTopic
Note: The subscription needs to be called after the initialization is completed, such as in the onSdkInitSuccess (4.3 data transparent transmission and initialization completed) function, or when the clientID (clientID acquisition method in 4.4clientID and token acquisition) is not empty

  // topic is the topic of the subscription, String type
 PushManager.getInstance().subscribeToTopic(topic, new ITopicListener() {
    @Override
    public void onFail(String msg) {
        Log.d("PUSH_", "Subscription failed:" + msg);
    }

    @Override
    public void onSuccess() {
        Log.d("PUSH_", "Subscription successful");
    }
});

Unsubscribe unsubscribeFromTopic

  // topic is the unsubscribed topic, String type
PushManager.getInstance().unsubscribeFromTopic(topic, new ITopicListener() {
    @Override
    public void onFail(String msg) {
        Log.d("PUSH_", "Unsubscribing failed:" + msg);
    }

    @Override
    public void onSuccess() {
        Log.d("PUSH_", "Cancel subscription successfully");
    }
});

#2.6 Set test AppId and AppKey

The current push platform supports the test mode. The test application and the officially released application package name can be the same, but the AppId and AppKey are different. The specific calling method is as follows:

// Determine if it is test mode
if (isTestMode) {
    PushManager.getInstance().setAppInfo(appId, appKey);
}

#2.7 Configuration modification

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();

        CoreUtil.init(getApplicationContext());
        PushManager.getInstance().setPushConfig(
                new PushConfig.Builder()
                        .setCheckInterval(60 * 60) // Set the interval for checking jobService, in seconds.
                        .setUploadInterval(2 * 60 * 60)// Set the wifi reporting interval in seconds
                        .setMobileUploadInterval(6 * 60 * 60)// Set mobile network reporting interval in seconds
                        .setCloseJobService(true)// Disable jobService reporting function
                        .setGroupId("group1") // Set Notification Group
                        .setMaxNotificationCount(4) // Set the maximum number of notifications displayed
                        .build());
       // push initialization
        PushManager.getInstance().init(getApplicationContext());
        // Pass-through registration
        BroadcastReceiver broadcastReceiver = new PushBroadcastReceiver();
        PushManager.getInstance().registerReceiver(getApplicationContext(), broadcastReceiver);
    }
}

setMaxNotificationCount indicates the maximum number of notifications displayed. If this value is exceeded, the earliest received push notification will be deleted.
setGroupId sets the notification group. Setting this will tile the received notifications.

#2.8 Notification switch

The SDK provides a check notification switch and a dialog box for displaying the notification switch page. The simple use is as follows:

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        checkNotificationSwitch();
    }

    private void checkNotificationSwitch() {
        // Check if notifications are on
        if (!NotificationAssistUtils.isOpenNotification(this)) {
            // Show user prompt dialog
        }
    }
}

The application can select the appropriate scene call.