5

私はすでにプッシュ通知を受け取っていますが、アプリ内通知を統合する必要があります。私は以下のなかったことを行うために:Facebookのアナリティクス - アプリ内通知が機能しない

は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()は、受信するRemoteMessageに、期待するJSONObjectのキーfb_push_cardが含まれていないように見えるため、falseを返します。

答えて

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

関連する問題