Firebase Cloud Messaging не отправляет дополнительные данные - PullRequest
0 голосов
/ 04 августа 2020

Я отправляю уведомление своему приложению с помощью Firebase Console, используя облачный обмен сообщениями firebase. Я могу получить уведомление, но не получаю дополнительных данных. Я хочу открыть другое действие с этими данными, но вот коды не повезло.

Изображение дополнительных данных Firebase Console:

image

import android.app.PendingIntent;
import android.content.Intent;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import com.saikari.bhojpurimovies.R;
import com.saikari.bhojpurimovies.activities.SplashActivity;

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    public MyFirebaseMessagingService() {
    }

    @Override
    public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);

        String title = remoteMessage.getNotification().getTitle();
        String body = remoteMessage.getNotification().getBody();
        String id =  remoteMessage.getData().get("id"); //not getting anything.
        Log.i("Id from notification", "onMessageReceived: "+id);
        if (remoteMessage.getData().size() > 0){
            showNotificationId(title, body, id);
            Log.i("Id from inside one", "onMessageReceived: "+id);
        }else {
            showNotification(title, body);
            Log.i("TAG can'getdata", "onMessageReceived: "+body+title);
        }
    }

    public void showNotification(String title, String message) {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "MyNotification")
                .setContentTitle(title)
                .setSmallIcon(R.drawable.ic_errain)
                .setAutoCancel(true)
                .setContentText(message);
        NotificationManagerCompat managerCompat =  NotificationManagerCompat.from(this);
        managerCompat.notify(999, builder.build());
    }

    public void showNotificationId(String title, String message, String id){
        Intent intent = new Intent(getApplicationContext(), SplashActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra("videoID", id);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "MyNotification")
                .setContentTitle(title+"check")
                .setSmallIcon(R.drawable.ic_launcher_background)
                .setAutoCancel(true)
                .setContentText(message+"check")
                .setContentIntent(pendingIntent);
        NotificationManagerCompat managerCompat =  NotificationManagerCompat.from(this);
        managerCompat.notify(999, builder.build());
    }


    @Override
    public void onNewToken(String s) {
        super.onNewToken(s);
    }


}

Это мое служебное объявление в файле манифеста.

<service
    android:name=".services.MyFirebaseMessagingService"
    android:exported="false">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT"/>
            </intent-filter>
</service>
...