0

Purpose: To open specific link in webview when user clicks on notification.

Working: When app is in foreground, user can click on notification and he is redirected to url.

Not Working: When app is not open or is in background, it is not working. When user clicks on notification, App is opened only, but user is not redirected to url mentioned.

I have tried to go through this link but I am not able to get the solution.

Here is the FirebaseMessagingService code as below:

public class FcmMessagingService extends FirebaseMessagingService {
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        // Also if you intend on generating your own notifications as a result of a received FCM
        // message, here is where that should be initiated. See sendNotification method below.
        String title = remoteMessage.getNotification().getTitle();
        final String message = remoteMessage.getNotification().getBody();
        String click_action = remoteMessage.getData().get("click_action");

        sendOnChannel1(title, message, click_action);
    }

    private void sendOnChannel1(String title, String messageBody, String click_action) {
        Intent activityIntent = new Intent(this, MainActivity.class);
        activityIntent.putExtra("linkz", click_action);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 1 /* Request code */, activityIntent, 0);

        String channelId = "fcm_default_channel";
        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId)
                .setSmallIcon(R.mipmap.ic_icon)
                .setContentTitle(title)
                .setContentText(messageBody)
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setCategory(NotificationCompat.CATEGORY_REMINDER)
                //.setColor(Color.BLUE)
                .setSound(defaultSoundUri)
                .setContentIntent(contentIntent)
                .setAutoCancel(true)
                .setOnlyAlertOnce(true);
                //.addAction(R.mipmap.ic_launcher, "Toast", actionIntent);

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        // Since android Oreo notification channel is needed.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(channelId,
                    "Channel human readable title",
                    NotificationManager.IMPORTANCE_DEFAULT);
            notificationManager.createNotificationChannel(channel);
        }

        notificationManager.notify(1 /* ID of notification */, notificationBuilder.build());
    }
}

Manifest:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<meta-data android:name="android.webkit.WebView.EnableSafeBrowsing"
    android:value="true" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_icon"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_icon"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
    android:hardwareAccelerated="true"
    android:usesCleartextTraffic="true">
    <activity
        android:name=".MainActivity"
        android:parentActivityName=".welcome"
        android:screenOrientation="portrait"
        android:configChanges="orientation|screenSize"
        android:exported="true">
        <intent-filter>
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
        <activity
            android:name=".welcome"
            android:screenOrientation="portrait"
            android:configChanges="orientation|screenSize"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />

                <data
                    android:scheme="http"
                    android:host="www.careturtle.com"
                    />

            </intent-filter>
        </activity>
     <service android:name=".FcmMessagingService"
        android:exported="false"
        android:directBootAware="true">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </service>

    <meta-data
        android:name="com.google.firebase.messaging.default_notification_icon"
        android:resource="@mipmap/ic_icon" />
    <activity
        android:name=".noconnection"
        android:screenOrientation="portrait">
    </activity>
</application>
Observer
  • 345
  • 1
  • 4
  • 21

1 Answers1

1

This might help beginners, Send API with Data Messages instead of Notification Messages.

Data Message:

{
  "to":"your_device_token",
  "data": {
      "title": "hello",
      "message": "test message",
      "url": "www.example.com",
  },
 "priority":"high"
}

Notification Message:

{
  "to":"your_device_token",
  "notification": {
      "title": "hello",
      "message": "test message",
  },
 "priority":"high"
}

Refernce: https://firebase.google.com/docs/cloud-messaging/android/receive

Observer
  • 345
  • 1
  • 4
  • 21