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

回答

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); 
    } 
} 

提到:https://github.com/facebook/FBNotifications

+1

您沒有提供任何新信息,只是複製了Facebook文檔中的代碼。這對我沒有幫助。 – AlvaroSantisteban