使用 SFMC 时无法取消通知

问题描述 投票:0回答:2

我正在使用 SalesForce Messaging Cloud (SFMC) SDK 推送通知以及 Firebase SDK。

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 
        

    }
}

通过

cancel
取消通知不起作用,而
cancelAll
则清除应用程序的所有通知。 我将 notificationIds 传递给
cancel
方法来取消通知,但显然,它不起作用。 我有什么遗漏的吗? 预先感谢。

android push-notification salesforce
2个回答
1
投票

问题解决了。 SFMC SDK 提供了取消通知的方法。

        import com.salesforce.marketingcloud.notifications.NotificationManager
    
        
        NotificationManager.cancelNotificationMessage(
                    context,
                    notificationMessage
                )
    

0
投票

这个可行,但你真的不需要整个消息。您可以直接从您的活动或数据源使用它:

val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager 
notificationManager.cancel("com.marketingcloud.salesforce.notifications.TAG", id)
© www.soinside.com 2019 - 2024. All rights reserved.