MainActivity получает дополнительную строку как ноль при открытии с уведомлением Pu sh - PullRequest
0 голосов
/ 25 января 2020

Я использую веб-приложение и хочу загрузить URL-адрес, полученный в уведомлении. Для этого я использую putExtra для получения URL в основной активности, но я получаю нулевое значение.

Вот мой код Код FcmMessagingService -

public class FcmMessagingService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        String title = remoteMessage.getNotification().getTitle();
        String message = remoteMessage.getNotification().getBody();
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        intent.putExtra("fcm_url", "here url will be there");
        PendingIntent pendingIntent =  PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);


        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this,"Message");
        notificationBuilder.setContentTitle(title);
        notificationBuilder.setContentText(message);
        notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
        notificationBuilder.setAutoCancel(true);
        notificationBuilder.setContentIntent(pendingIntent);
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel("Message", "Default channel", NotificationManager.IMPORTANCE_DEFAULT);
            notificationManager.createNotificationChannel(channel);
        }
        notificationManager.notify(0, notificationBuilder.build());
    }
}

Это мой код MainActivity onCreate -

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    Intent intent = getIntent();
    final String fcm_message = intent.getStringExtra("fcm_url");

    if(fcm_message!=null){
        Log.d("Fcm_url",fcm_message);
    }
    else{
        Log.d("Fcm_url","This is null");
    } 
    ...

Когда я открываю приложение напрямую, logcat печатает This is null, что нормально, но когда я отправляю уведомление и открываю приложение, нажимая уведомление, оно снова печатает This is null, что я делаю неправильно?

Обновление

Когда я добавляю Log.d.. внутри моего onMessageRecieved, его выполнение не выполняется, см.

public void onMessageReceived(RemoteMessage remoteMessage) {
        Log.d("on_message_recieved","fcm message recieved");
        ....

, но это не печать после получения уведомления ???

В моем ManiFest я добавил это -

<service
            android:name=".FcmMessagingService"
            android:permission="false">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>

Кажется, что он не выполняет мой пользовательский FcmMessagingService, но почему ??

Ответы [ 2 ]

0 голосов
/ 25 января 2020

Удалите эту ссылку и попробуйте.

    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
0 голосов
/ 25 января 2020

Для получения значения из FCM в MainActivity попробуйте следующий код.

if (getIntent().getExtras() != null) {
          String fcm_url=  getIntent().getExtras().getString("fcm_url")
        } 
...