5
我已經收到推送通知,但現在我必須整合應用內通知。爲了做到這一點我做了以下內容:Facebook Analytics - 應用內通知不起作用
新增的lib在gradle這個文件:
compile "com.facebook.android:notifications:${libs.fb_in_app_notifications}"
:所做的更改
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Other non related code
NotificationsManager.presentCardFromNotification(this);
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
NotificationsManager.presentCardFromNotification(this);
}
和調整,我已經存在FirebaseMessagingService
來區分推送通知和應用內通知。
public class MyFirebaseMessagingService extends FirebaseMessagingService {
public static final String PUSH_NOTIFICATION_EXTRA = "push";
private static final String NOTIFICATION_TITLE = "title";
private static final String NOTIFICATION_BODY = "body";
public MyFirebaseMessagingService() {
}
@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());
}
Timber.d("onMessageReceived");
if (NotificationsManager.canPresentCard(data)) {
Timber.d("canPresentCard -> in-app notification");
NotificationsManager.presentNotification(
this,
data,
new Intent(getApplicationContext(), MainActivity.class)
);
} else {
Timber.d("Can not present card -> push notification");
Context context = this.getApplicationContext();
Intent defaultAction = new Intent(context, MainActivity.class)
.setAction(Intent.ACTION_DEFAULT)
.putExtra(PUSH_NOTIFICATION_EXTRA, data);
String title = data.getString(NOTIFICATION_TITLE);
String body = data.getString(NOTIFICATION_BODY);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.launcher)
.setContentTitle(title == null ? "" : title)
.setContentText(body == null ? "" : body)
.setAutoCancel(true)
.setContentIntent(PendingIntent.getActivity(
context,
0,
defaultAction,
PendingIntent.FLAG_UPDATE_CURRENT
));
NotificationManager mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(123, mBuilder.build());
}
}
}
問題是方法NotificationsManager.canPresentCard()
總是返回false。任何線索爲什麼會發生這種情況?
編輯1:方法NotificationsManager.canPresentCard()
返回false,因爲它收到的RemoteMessage
似乎沒有包含它所期望的JSONObject的密鑰fb_push_card
。
您沒有提供任何新信息,只是複製了Facebook文檔中的代碼。這對我沒有幫助。 – AlvaroSantisteban