Уведомление Android не открывает активность? - PullRequest
0 голосов
/ 06 июля 2019

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

public class MyMessagingService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
        showNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody()); 
    }

    public void showNotification(String title, String message) { 

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "MyNotifitcation")
                .setContentTitle(title)
                .setSmallIcon(R.drawable.ic_launcher_background)
                .setAutoCancel(true)
                .setContentText(message);

        NotificationManagerCompat manager = NotificationManagerCompat.from(this);
        manager.notify(999, builder.build());
    }
}



public class MainActivity extends AppCompatActivity {
    private String TAG = MainActivity.class.getSimpleName();
    String title;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel =
                    new NotificationChannel("MyNotifitcation", "MyNotifitcation", NotificationManager.IMPORTANCE_DEFAULT);

            NotificationManager manager = getSystemService(NotificationManager.class);
            manager.createNotificationChannel(channel);
        }

        FirebaseMessaging.getInstance().subscribeToTopic("general").addOnCompleteListener(new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {
                String msg = "Successfull";
                if (!task.isSuccessful()) {
                    msg = "Failed";
                }
//                Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
            }
        });
    }
}

Ответы [ 2 ]

0 голосов
/ 11 июля 2019

См. Код ниже.Открытие деятельности. оригинал почтового кредита

NotificationManager notificationManager = (NotificationManager) context
        .getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);

Intent notificationIntent = new Intent(context, HomeActivity.class);

notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
        | Intent.FLAG_ACTIVITY_SINGLE_TOP);

PendingIntent intent = PendingIntent.getActivity(context, 0,
        notificationIntent, 0);

notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
0 голосов
/ 06 июля 2019

Для уведомления нажмите кнопку Добавить ожидающее намерение для создателя уведомлений. и для добавления изображения / значка к уведомлению используйте setSmallIcon () в построителе уведомлений. ниже мой код уведомления.

private void sendNotification(String body, String title) {
        Intent intent = new Intent(this, NotificationActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra("action_type", "notify");
        PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 0, intent,
                PendingIntent.FLAG_ONE_SHOT);
        NotificationCompat.Builder notificationBuilder = new
                NotificationCompat.Builder(this,"channel")
                .setSmallIcon(R.drawable.logo)
//                .setContent(contentView)
                .setContentTitle("title")
                .setContentText("body")
                .setAutoCancel(true)
                .setContentIntent(pendingIntent);
        Notification notification = notificationBuilder.build();
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notificationManager.notify(0, notification);
}
...