Код: Выделить всё
SFMC Config BuilderКод: Выделить всё
...
configBuilder.setNotificationCustomizationOptions(NotificationCustomizationOptions.create((context, notificationMessage) -> {
NotificationCompat.Builder builder = NotificationUI.setSFMCNotification(context, notificationMessage); //for customization purpose, delegating to NotificationUI
return builder;
})
);
...
Код: Выделить всё
FirebaseMessagingServiceКод: Выделить всё
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
...
if (PushMessageManager.isMarketingCloudPush(remoteMessage)) { //if SFMC then handle in sdk
MarketingCloudSdk.requestSdk(marketingCloudSdk -> marketingCloudSdk.getPushMessageManager().handleMessage(remoteMessage));
}
...
}
Код: Выделить всё
NotificationUIКод: Выделить всё
@NotNull
public static NotificationCompat.Builder setSFMCNotification(Context context, NotificationMessage notificationMessage) {
NotificationCompat.Builder builder;
NotificationChannel notificationChannel;
boolean isMediaAvailable = notificationMessage.mediaUrl() != null && !notificationMessage.mediaUrl().isEmpty();
// If we're running on Android O we create a notification channel
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
builder = new NotificationCompat.Builder(context, context.getString(R.string.default_notification_channel_id));
} else {
notificationChannel = new NotificationChannel(SFMC_PUSH_NOTIFICATION_CHANNEL_ID,
context.getString(R.string.sfmc_notification_channel), NotificationManager.IMPORTANCE_DEFAULT);
getNotificationManager(context).createNotificationChannel(notificationChannel);
builder = new NotificationCompat.Builder(context, SFMC_PUSH_NOTIFICATION_CHANNEL_ID);
}
if (isMediaAvailable) {
bitmapImage = getBitmapfromUrl(notificationMessage.mediaUrl());
builder = showSFMCPushNotification(context, notificationMessage, builder, true);
} else {
builder = showSFMCPushNotification(context, notificationMessage, builder, false);
}
return builder;
}
static private NotificationCompat.Builder showSFMCPushNotification(Context context, NotificationMessage notificationMessage, @NotNull NotificationCompat.Builder builder, boolean isMedia) {
int notificationId = getUniqueNotificationID();//new Random().nextInt();
int abridged = notificationId % 1000;
Log.e("ASD", String.valueOf(notificationId));
PendingIntent pendingIntent = PendingIntent.getActivity(context, abridged, new Intent(context, MainActivity.class),
FLAG_UPDATE_CURRENT);
builder.setContentIntent(
com.salesforce.marketingcloud.notifications.NotificationManager.redirectIntentForAnalytics(context, pendingIntent,
notificationMessage,
true
)
)
.setPriority(Notification.PRIORITY_HIGH)
.setContentTitle(notificationMessage.title())
.setSmallIcon(getNotificationSmallIcon())
.setContentText(notificationMessage.subtitle())
.setAutoCancel(true);
if (isMedia) {
Intent intent = new Intent(context, SFMCActionReceiver.class);
intent.putExtra(EXTRA_SFMC_NOTIFICATION_ID_FOR_CANCEL_ACTION, notificationId);
intent.putExtra("ASD", abridged);
PendingIntent pendingAction = PendingIntent.getBroadcast(context, notificationId, intent, FLAG_UPDATE_CURRENT);
builder.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(bitmapImage).bigLargeIcon(null)).setLargeIcon(bitmapImage);
builder.addAction(0, "ORDER NOW", pendingAction);
builder.addAction(0, "DELETE", pendingAction);
}
return builder;
}
Код: Выделить всё
SFMCActionReceiverКод: Выделить всё
class SFMCActionReceiver : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
//Cancel your ongoing Notification
val nm =
context?.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val notificationIdToCancel = if (intent?.extras != null) intent.extras!!
.getInt(EXTRA_SFMC_NOTIFICATION_ID_FOR_CANCEL_ACTION) else 0
val abridged = if (intent?.extras != null) intent.extras!!.getInt("ASD") else 0
Log.i(
"ASD",
"dismissing sfmc notification Id:$notificationIdToCancel $abridged"
)
nm.cancel( abridged); //doesnt work
nm.cancel( notificationIdToCancel) //doesnt work
nm.cancelAll() //works
}
}
Я передаю идентификаторы уведомлений для отмены
code> для отмены уведомления, но, судя по всему, он не работает.
Я что-то упускаю?
Заранее спасибо.
Подробнее здесь: https://stackoverflow.com/questions/682 ... using-sfmc