【问题标题】:Facebook Analytics - In-app notification not workingFacebook 分析 - 应用内通知不起作用
【发布时间】:2017-10-18 10:34:41
【问题描述】:

我已经让推送通知工作了,但现在我必须集成应用内通知。为了做到这一点,我做了以下事情:

在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

【问题讨论】:

    标签: android facebook-analytics facebook-analytics-push


    【解决方案1】:

    请使用下面的代码,它对我来说非常适合: 将此添加到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

    【讨论】:

    • 您没有提供任何新信息,只是从 Facebook 文档中复制了代码。这对我没有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多