shahriar
shahriar

Reputation: 33

Firebase push notification data handling while the app is in background

i am trying to setup firebase push notification for my webview android app. My target is, i will send a url in the notification and it will open in the app. here is my problem.

following is the php code im using to send the message

<?php

// API access key from Google API's Console
define( 'API_ACCESS_KEY', 'XXXXX' );
$id='XXXXXX';
$registrationIds = array($id);
// prep the bundle
$msg = array
(
'body'  => 'Your post has received a reply',
'title'     => 'Title of the post here',
);

$data = array
(
        'url'=>'http://XXXXXXXX.com/forum/viewtopic.php?f=2&p=21421#p21421'

);

$fields = array
(
    'registration_ids'  => $registrationIds,
    'notification'          => $msg,
    'data'          => $data

);

$headers = array
(
    'Authorization: key=' . API_ACCESS_KEY,
    'Content-Type: application/json'
);

$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send' );
curl_setopt( $ch,CURLOPT_POST, true );
curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
$result = curl_exec($ch );
curl_close( $ch );

echo $result;

And this is my manifest

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity" android:configChanges="orientation|screenSize">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".MyFirebaseMessagingService$NotificationBar" />

    <service android:name=".MyFirebaseMessagingService">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT"/>
        </intent-filter>
    </service>
    <service android:name=".FirebaseIDService">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
        </intent-filter>
    </service>
</application>

and this is my receiver

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    public static String url;
    public static final String TAG = "FCM Service";
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
        Log.d(TAG, "From: " + remoteMessage.getFrom());
        Log.d(TAG, "Data: " + remoteMessage.getData());
        url = remoteMessage.getData().get("url");
        Log.d(TAG, "URL: " + url);

    }
    public class NotificationBar extends Activity {
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_notification_bar);

        }
    }
}

from here i pass the url to main activity. Now the problem is, if the app is in foreground and i send a message, i can see the url in the log. But when the app is in background and i send a message, i only see the notification in the status and nothing in the log. if i tap on the notification, it just open the app without passing the url.

could you please help? please be as detail as possible as i have almost zero idea about android programming.

thanks!

Upvotes: 1

Views: 491

Answers (1)

random
random

Reputation: 10309

Please see explanation here:

onMessageReceived is provided for most message types, with the following exceptions:

Notification messages delivered when your app is in the background. In this case, the notification is delivered to the device’s system tray. A user tap on a notification opens the app launcher by default.

Messages with both notification and data payload, both background and foreground. In this case, the notification is delivered to the device’s system tray, and the data payload is delivered in the extras of the intent of your launcher Activity.

You should check for data payload in the extras of the intent of your launcher Activity which means if HomeActivity is your launcher activity in manifest you should check inside HomeActivity's onCreate method:

    Intent intent = getIntent();
    String url = intent.getStringExtra("url");
    ... 

Upvotes: 1

Related Questions