FCM Android数据讯息

时间:2016-12-06 13:43:28

标签: android firebase firebase-cloud-messaging

即使应用程序在后台,我也会收到通知消息,但每当收到通知时,它都会自动打开活动(即使用户没有点击通知)。

我的onMessageReceived

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    Bundle b = new Bundle();
    if (data.containsKey("click_action")) {
        if(data.containsValue("postlike") || data.containsValue("comment") || data.containsValue("newpost")){
            b.putString("PostId", data.get("post_id").toString());
            Firebase_Action_Click.startActivity(data.get("click_action"), b, this);
        } else {
            Intent intent = new Intent(this, ActivityMain.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            pendingIntent = PendingIntent.getActivity(this, 0, intent,PendingIntent.FLAG_ONE_SHOT);
        }
    }

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.notification_icon)
        .setContentText(data.get("body"))
        .setAutoCancel(true)
        .setSound(defaultSoundUri)
        .setContentIntent(pendingIntent);

    NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0, notificationBuilder.build());
}

在我的Firebase_Action_Click

public static void startActivity(String className, Bundle extras, Context context) {
    Class cls = null;
    try {
        cls = Class.forName(className);
    } catch(ClassNotFoundException e){
        //means you made a wrong input in firebase console
    }

    Intent i = new Intent(context, cls);
    i.putExtras(extras);
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(i);
}

在我的启动器活动中,我添加了这些行,

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    if (intent.hasExtra("click_action")) {
        Firebase_Action_Click.startActivity(intent.getStringExtra("click_action"), intent.getExtras(), this);
    }
}

一切顺利,但我遇到的唯一问题是,即使应用程序在后台,我收到通知,它会自动打开应用程序。

我希望只有在用户点击通知时才能执行相同的操作。

任何人都可以帮助我,提前致谢

2 个答案:

答案 0 :(得分:1)

它会触发Activity,因为您从Activity函数启动了onMessageReceived。因此,如果您想在点击通知时启动Activity,则可能需要将Activity作为PendingIntent启动。

我想建议您在代码中进行简单修改。 startActivity中的Firebase_Action_Click功能可能会返回其创建的Intent,如下所示。

public static Intent startActivity(String className, Bundle extras, Context context) {
    Class cls = null;
    try {
        cls = Class.forName(className);
    } catch(ClassNotFoundException e){
        //means you made a wrong input in firebase console
    }

    Intent i = new Intent(context, cls);
    i.putExtras(extras);
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    return i; // Return the intent from here. 
}

现在,在onMessageReceived函数中,您需要在PendingIntent内设置if(data.containsValue("postlike") || data.containsValue("comment") || data.containsValue("newpost")) - 这个if语句,就像您为else部分设置的语句一样。所以你的代码可能看起来像这样。

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    Bundle b = new Bundle();

    if (data.containsKey("click_action")) {
        if(data.containsValue("postlike") || data.containsValue("comment") || data.containsValue("newpost")) {
            b.putString("PostId", data.get("post_id").toString());
            Intent intent = Firebase_Action_Click.startActivity(data.get("click_action"), b, this);

            // Now add the flags and create pendingIntent here
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);

        } else {
            Intent intent = new Intent(this, ActivityMain.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
        }
    }

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.notification_icon)
        .setContentText(data.get("body"))
        .setAutoCancel(true)
        .setSound(defaultSoundUri)
        .setContentIntent(pendingIntent);

    NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0, notificationBuilder.build());
}

现在您的通知系统应该可以正常工作。

答案 1 :(得分:0)

简单,sóajustaro flag FLAG_ACTIVITY_NEW_TASK se o metodo onMessageReceived estiver dentro de uma classe que nao seja activity:D~

没有metodo受体:

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    ComponentName componenteName = new ComponentName(this, YourActivity.class);
    intent.setComponent(componenteName);
    startActivity(intent);
}

没有seu宣言:

<activity android:name=".YourActivity" />