получить Intent Extra от нажатия уведомлений через Firebase - PullRequest
0 голосов
/ 29 марта 2020

Уведомление приходит в центр уведомлений с изображением (даже приложение закрыто | приложение находится в фоновом режиме), и всякий раз, когда я нажимаю на уведомление, действие открывает его, но нет данных о намерениях.

Я всегда получаю ноль, когда пытаюсь чтобы получить намерение дополнительно. Независимо от того, что я стараюсь передавать данные через Bundle или напрямую передавать в intent.setExtra, я всегда получаю нулевое значение.


У меня есть класс обработчика Firebase

public class MyFirebaseInstanceService extends FirebaseMessagingService {

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

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        //super.onMessageReceived(remoteMessage);
        String msg = remoteMessage.getNotification().getBody();
        String title = remoteMessage.getNotification().getTitle();
        String channel = remoteMessage.getNotification().getChannelId();
        Holder holder = new Holder();
        holder.setTitle(title);
        holder.setMsg(msg);
        holder.setChannel(channel);
        if (remoteMessage.getData().size() > 0) {
            Map<String, String> params = remoteMessage.getData();
            holder.setId(params.get("id"));
            holder.setImgUrl(params.get("imgurl"));
            holder.setType(params.get("type"));
        } else {
            int r = new Random().nextInt();
            holder.setId(r + "");
            holder.setType("bigtext");
        }
        sendMyNotification(holder, remoteMessage);
    }

    private void sendMyNotification(Holder holder, RemoteMessage remoteMessage) {
        String clickAction = remoteMessage.getNotification().getClickAction();
        Intent intent = new Intent(this, NotificationReadActivity.class);
        if (clickAction != null) {
            if (!clickAction.isEmpty() && clickAction.length() > 5)
                intent = new Intent(clickAction);
        }
        intent.putExtra("obj", holder);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.setAction(holder.getId() + "");
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, holder.getChannel());
        notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
        notificationBuilder.setAutoCancel(true);
        notificationBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
        notificationBuilder.setChannelId(holder.getChannel());
        if (holder.getType().equals("bigpic")) {
            notificationBuilder.setStyle(new NotificationCompat.BigPictureStyle());
            Bitmap bitmap = getBitmapfromUrl(holder.getImgUrl());
            if (bitmap != null) {
                notificationBuilder.setLargeIcon(bitmap);
            }
        } else {
            notificationBuilder.setStyle(new NotificationCompat.BigTextStyle());
        }
        notificationBuilder.setContentTitle(holder.getTitle());
        notificationBuilder.setContentText(holder.getMsg());
        PendingIntent pendingIntent = PendingIntent.getActivity(this, holder.getId(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
        notificationBuilder.setContentIntent(pendingIntent);
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                NotificationChannel channel = new NotificationChannel(holder.getChannel(), "Default", NotificationManager.IMPORTANCE_DEFAULT);
                channel.enableLights(true);
                channel.enableVibration(true);
                channel.setLightColor(Color.RED);
                channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
                notificationManager.createNotificationChannel(channel);
            }
            notificationManager.notify(holder.getId(), notificationBuilder.build());
        } catch (Exception ignored) {
        }
    }

    private Bitmap getBitmapfromUrl(String imageUrl) {
        try {
            URL url = new URL(imageUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            return BitmapFactory.decodeStream(input);
        } catch (Exception ignored) {
            return null;
        }
    }
}

, тогда у меня есть одно действие

public class NotificationReadActivity extends AppCompatActivity {

    TextView title, desc;
    AppCompatImageView img;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notification_read);
        title = findViewById(R.id.title);
        desc = findViewById(R.id.desc);
        img = findViewById(R.id.img);
        handleIntent(getIntent());
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        handleIntent(intent);
    }

    private void handleIntent(Intent intent) {
        Bundle bundle = intent.getExtras();
        if (intent.hasExtra("obj")) {
            Holder holder = (Holder) intent.getSerializableExtra("obj");
            if (holder.getType().equals("bigpic")) {
                Glide.with(this).load(holder.getImgUrl()).into(img);
            } else
                img.setVisibility(View.GONE);
            title.setText(holder.getTitle());
            desc.setText(holder.getMsg());
        } else if (bundle.containsKey("obj")) {
            Holder holder = (Holder) bundle.getSerializable("obj");
            if (holder.getType().equals("bigpic")) {
                Glide.with(this).load(holder.getImgUrl()).into(img);
            } else
                img.setVisibility(View.GONE);
            title.setText(holder.getTitle());
            desc.setText(holder.getMsg());
        } else {
            title.setText("Null");
            //finish();
        }
    }
}

в манифесте Я также настроил правильно

<activity
   android:name=".NotificationReadActivity"
   android:excludeFromRecents="true"
   android:label="@string/notification_center"
   android:launchMode="singleTop">
      <intent-filter>
         <action android:name="com.full_package.NotificationReadActivity.TARGET_NOTIFICATION" />

         <category android:name="android.intent.category.DEFAULT" />
      </intent-filter>
</activity>
<service
   android:name=".Fcm.MyFirebaseInstanceService"
   android:exported="true"
   android:permission="com.google.android.c2dm.permission.SEND">
      <intent-filter>
         <action android:name="com.google.firebase.MESSAGING_EVENT" />
         <action android:name="com.google.android.c2dm.intent.RECEIVE" />
      </intent-filter>
</service>

1 Ответ

0 голосов
/ 29 марта 2020

Я пришел к единственному решению - передать строку или int или любой другой тип данных, кроме объекта.

Intent intent = new Intent(this, NotificationReadActivity.class);
intent.putExtra("key1", "some string"); //this is correct
intent.putExtra("key2", 123); //this is correct
intent.putExtra("key3", any type of data); //this is correct

intent.putExtra("object", some_object); //this is in-correct
...