Android通知未开启活动?

时间:2019-07-06 06:35:10

标签: java android firebase android-intent firebase-cloud-messaging

我有一个使用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 :(得分:0)

对于通知,单击将待处理的意图添加到通知构建器。要将图像/图标添加到通知中,请在通知构建器中使用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);
}

答案 1 :(得分:0)

请参见以下代码。开幕活动。  original post credit

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);