Пуш-уведомления RuStore в apk-webview: SDK, сервис приёма, канал
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
package ru.ua1zbe.bus127;
|
||||
|
||||
import android.app.Notification;
|
||||
import android.app.NotificationManager;
|
||||
import android.util.Log;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import ru.rustore.sdk.pushclient.messaging.exception.RuStorePushClientException;
|
||||
import ru.rustore.sdk.pushclient.messaging.model.RemoteMessage;
|
||||
import ru.rustore.sdk.pushclient.messaging.service.RuStoreMessagingService;
|
||||
import ru.ua1zbe.bus127.push.PushStore;
|
||||
|
||||
/**
|
||||
* Приём пуш-уведомлений RuStore.
|
||||
*
|
||||
* Если сервер прислал секцию notification — SDK показывает уведомление сам.
|
||||
* Если пришли только data (title/body) — показываем уведомление сами.
|
||||
*/
|
||||
public class PushMessagingService extends RuStoreMessagingService {
|
||||
|
||||
@Override
|
||||
public void onNewToken(String token) {
|
||||
Log.i(TAG, "onNewToken: " + token);
|
||||
PushStore.setToken(this, token);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMessageReceived(RemoteMessage message) {
|
||||
if (message.getNotification() != null) {
|
||||
Log.i(TAG, "push displayed by SDK: " + message.getData());
|
||||
return;
|
||||
}
|
||||
Map<String, String> data = message.getData();
|
||||
String title = data.get("title");
|
||||
if (title == null || title.isEmpty()) {
|
||||
title = getString(R.string.app_name);
|
||||
}
|
||||
String body = data.get("body");
|
||||
if (body == null) {
|
||||
body = "";
|
||||
}
|
||||
if (!title.isEmpty() || !body.isEmpty()) {
|
||||
showPush(title, body, data.get("url"));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDeletedMessages() {
|
||||
Log.i(TAG, "onDeletedMessages");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(List<? extends RuStorePushClientException> errors) {
|
||||
for (RuStorePushClientException e : errors) {
|
||||
Log.w(TAG, "error", e);
|
||||
}
|
||||
}
|
||||
|
||||
private void showPush(String title, String body, String url) {
|
||||
Notification.Builder b = new Notification.Builder(this, Bus127App.CHANNEL_ID)
|
||||
.setContentTitle(title)
|
||||
.setContentText(body)
|
||||
.setSmallIcon(R.mipmap.ic_launcher)
|
||||
.setAutoCancel(true);
|
||||
if (url != null && !url.isEmpty()) {
|
||||
b.setContentIntent(PushIntents.web(this, url));
|
||||
}
|
||||
getSystemService(NotificationManager.class).notify(NOTIFICATION_ID, b.build());
|
||||
}
|
||||
|
||||
private static final String TAG = "RuStorePush";
|
||||
private static final int NOTIFICATION_ID = 127;
|
||||
}
|
||||
Reference in New Issue
Block a user