在背景中通过“ pendingIntent”推送通知

时间:2019-01-27 14:17:28

标签: android firebase firebase-cloud-messaging android-notifications

我希望在发生点击事件时,将使用适当的URL打开新窗口 我尝试例如:

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    public static final String TAG = MyFirebaseMessagingService.class.getSimpleName();
    private static final String ADMIN_CHANNEL_ID = "admin_channel";
    private NotificationManager notificationManager;
    private PendingIntent pendingIntent;

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse("https://www.udacity.com/"));

        pendingIntent = PendingIntent.getActivity(this,
                0, intent, PendingIntent.FLAG_ONE_SHOT);

        if (remoteMessage.getData().isEmpty()) //when the app in foreground
            showNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());
        else{                                 //when the app in background
            Map<String, String> data = remoteMessage.getData();
            showNotification(data.get("title"), data.get("body"));
        }
    }

    private void showNotification(String title, String body) {
        notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O)
            setupChannels();
        NotificationCompat.Builder notificationBuilder =
                new NotificationCompat.Builder(this, ADMIN_CHANNEL_ID)
                        .setAutoCancel(true)
                        .setDefaults(Notification.DEFAULT_ALL)
                        .setWhen(System.currentTimeMillis())
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setContentTitle(title)
                        .setContentText(body)
                        .setContentInfo("Info")
                        .setContentIntent(pendingIntent);

        notificationManager.notify(new Random().nextInt(), notificationBuilder.build());
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    private void setupChannels() {
        CharSequence adminChannelName = getString(R.string.notifications_admin_channel_name);
        String adminChannelDescription = getString(R.string.notifications_admin_channel_description);

        NotificationChannel adminChannel;
        adminChannel = new NotificationChannel(ADMIN_CHANNEL_ID, adminChannelName, NotificationManager.IMPORTANCE_LOW);
        adminChannel.setDescription(adminChannelDescription);
        adminChannel.enableLights(true);
        adminChannel.setLightColor(Color.RED);
        adminChannel.enableVibration(true);
        if (notificationManager != null) {
            notificationManager.createNotificationChannel(adminChannel);
        }
    }
}

当应用程序处于前台模式时,它可以正常运行,但处于后台模式时,因此在通知上单击事件会打开mainActivity类,并且无需注意:.setContentIntent(pendingIntent);线

解决该问题的任何建议都会得到评估

0 个答案:

没有答案