Android推送通知导航到内部Webview页面

时间:2017-08-17 09:52:42

标签: android webview push-notification single-page-application

我有一个接收推送通知的Android应用程序。它是webview中的单页面应用程序。现在点击推送通知,它会打开应用程序。我目前希望在特定的推送通知中导航到Web浏览器或应用程序的特定部分。

    @Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    if (remoteMessage == null) {
        return;
    }

    if (remoteMessage.getData().size() > 0) {
        // Message contains data payload
        // Called when app is in background, foreground or is closed
        String message = remoteMessage.getData().get("body");

        if (this.validator.isNull(message) || this.validator.isStringEmpty(message)) {
            return;
        }

        showNotification(message);
    }
}


private void showNotification(String messageBody) {
    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

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

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_notification)
            .setColor(getResources().getColor(R.color.notificationColor))
            .setContentTitle(getResources().getString(R.string.notificationContentTitle))
            .setContentText(messageBody.replaceAll("::", " "))
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .setTicker(messageBody.replaceAll("::", " "));

    NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
    String[] messageLines = messageBody.split("::");

    // Sets a title for the Inbox in expanded layout
    inboxStyle.setBigContentTitle(getResources().getString(R.string.notificationContentTitle));
    int numberOfRows = 0;

    // Moves messageLines into the expanded layout
    for (int i = 0; i < messageLines.length; i++) {
        if (messageLines[i].length() > 1) {

            // Maximum number of rows can be 6
            if (numberOfRows < 7) {
                inboxStyle.addLine(messageLines[i]);
                numberOfRows++;
            }
        }
    }

    if (messageLines.length > 0) {
        notificationBuilder.setStyle(inboxStyle);
    }

    int notificationId = getRandomId();

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(notificationId, notificationBuilder.build());
}

提前谢谢你。

0 个答案:

没有答案