有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Android通知不打开活动?

我有一个使用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();
            }
        });
    }
}

共 (0) 个答案