如何解决使用 SFMC 时无法取消通知
我正在使用 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,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,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
方法以取消通知,但显然它不起作用。
有什么我想念的吗?
提前致谢。
解决方法
解决了这个问题。 SFMC SDK 提供了取消通知的方法。
import com.salesforce.marketingcloud.notifications.NotificationManager
NotificationManager.cancelNotificationMessage(
context,notificationMessage
)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。