[英]Facebook in-app notification not working
这是我对应用内通知的集成
FCM服务
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String NOTIFICATION_CHANNEL_ID = "GrowFit";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Bundle data = new Bundle();
for (Map.Entry<String, String> entry : remoteMessage.getData().entrySet()) {
data.putString(entry.getKey(), entry.getValue());
}
Context context = this.getApplicationContext();
Intent defaultAction = new Intent(context, Dashboard.class)
.setAction(Intent.ACTION_DEFAULT)
.putExtra("push", data);
String title = data.getString("title");
String body = data.getString("body");
Vars.bundle = data;
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Grow Fit", NotificationManager.IMPORTANCE_HIGH);
notificationChannel.setDescription("GrowFit Notifications");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.BLUE);
notificationChannel.enableVibration(true);
notificationChannel.canShowBadge();
if (notificationManager != null) {
notificationManager.createNotificationChannel(notificationChannel);
}
}
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID)
.setSmallIcon(R.drawable.ic_stat_growfit)
.setContentTitle(title == null ? "Hey" : title)
.setContentText(body == null ? "Having a good day?" : body)
.setAutoCancel(true)
.setContentIntent(PendingIntent.getActivity(
context,
0,
defaultAction,
PendingIntent.FLAG_UPDATE_CURRENT
));
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (mNotificationManager != null) {
mNotificationManager.notify((int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE), mBuilder.build());
}
NotificationsManager.presentNotification(this, data, new Intent(getApplicationContext(), Dashboard.class), new NotificationsManager.NotificationExtender() {
@Override
public Notification.Builder extendNotification(@NonNull Notification.Builder builder) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
}
return mBuilder;
}
});
}
(处理意图的单个顶级活动 )
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
checkInAppPush(intent);
checkFBPush();
}
private void checkInAppPush(Intent intent) {
NotificationsManager.presentCardFromNotification(this, intent);
}
该意图具有附加功能,但不会呈现应用内通知。 我在这里想念什么吗?
您无需使用android Notification Manager即可呈现Facebook应用内通知,也无需使用fb应用内推送通知>>您必须集成fb-notification库>> https://github.com/facebook/FBNotifications集成fb通知后,需要在onMessageReceived方法中编写NotificationManager.canPresentCard(data)
Bundle data = new Bundle();
for (Map.Entry<String, String> entry : remoteMessage.getData().entrySet()) {
data.putString(entry.getKey(), entry.getValue());
}
Log.d(TAG, "onMessageReceived: "+ data.toString());
if (NotificationsManager.canPresentCard(data)) {
Log.d(TAG, "onMessageReceived: " + "PresentCard");
NotificationsManager.presentNotification(
this,
data,
new Intent(getApplicationContext(), MainActivity.class)
//MainActivity.getIntent(getApplicationContext(),data)
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.