5

Tôi đã nhận được thông báo đẩy hoạt động, nhưng bây giờ tôi phải tích hợp các thông báo trong ứng dụng . Để làm được điều mà tôi đã làm như sau:Facebook Analytics - Thông báo trong ứng dụng không hoạt động

Added lib trong file gradle:

compile "com.facebook.android:notifications:${libs.fb_in_app_notifications}"

thay đổi Made in hoạt động chính của tôi:

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

Và điều chỉnh của tôi đã tồn tại FirebaseMessagingService để phân biệt các thông báo đẩy từ thông báo trong ứng dụng.

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

Vấn đề là phương pháp NotificationsManager.canPresentCard() luôn trả về false. Bất kỳ đầu mối nào tại sao điều này có thể xảy ra?

Chỉnh sửa 1: Phương thức NotificationsManager.canPresentCard() trả về false vì RemoteMessage mà nó nhận được dường như không chứa khóa fb_push_card cho JSONObject mà nó mong đợi.

Trả lời

0

Hãy Sử dụng mã dưới đây nó làm việc hoàn hảo cho tôi: Thêm phần này vào build.gradle:

compile 'com.facebook.android:notifications:1.+' 

và thêm mã này dưới đây:

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

Trong Hoạt động của bạn Thêm Bộ luật này:

public class MainActivity extends AppCompatActivity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    NotificationsManager.presentCardFromNotification(this); 
    } 
} 

Tham khảo: https://github.com/facebook/FBNotifications

+1

Bạn không cung cấp bất kỳ thông tin mới nào, chỉ cần sao chép mã từ tài liệu trên Facebook. Điều đó không giúp tôi. – AlvaroSantisteban

Các vấn đề liên quan