我已经推送推送通知,但现在我必须整合应用内通知。为此,我做了以下事情:
在gradle文件中添加了lib:
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
。
答案 0 :(得分:0)
请使用以下代码,它对我来说非常适合:
将其添加到build.gradle
:
compile 'com.facebook.android:notifications:1.+'
并在下面添加以下代码:
@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());
}
NotificationsManager.presentNotification(
this,
data,
new Intent(getApplicationContext(), MainActivity.class)
);
}
在您的活动中添加此代码:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
NotificationsManager.presentCardFromNotification(this);
}
}