Varsha Vaidywan
Varsha Vaidywan

Reputation: 13

How to send data messages to single device using FCM in android

I'm new at using Firebase cloud messaging and want to send a message or notification with data to a single device. I've sent notifications to all devices but can't figure out how to send it to a single targeted device. I've looked through many websites and tutorials but no luck.

From one tutorial I was following, I wrote the following code in my

MainActivity.java :

package in.w3dev.fcm2;

import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import com.google.firebase.iid.FirebaseInstanceId;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    @Override
     protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);

     FirebaseInstanceId.getInstance().getInstanceId()
            .addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
                @Override
                public void onComplete(@NonNull Task<InstanceIdResult> task) {
                    if (!task.isSuccessful()) {
                        return;
                    }

                    String token = task.getResult().getToken();
                    String msg = getString(R.string.fcm_token, token);
                    Log.d(TAG, msg);

                }
            });

    }
}

But it shows error : can't resolve symbol getInstanceId(), InstanceResultId. All the things in bold aren't being resolved for some reason. I've already made services extending FirebaseInstanceIdService and FirebaseMessagingService correctly as described in the FCM window on the right side in the android studio. Tell me how to resolve these errors or another way to send messages to single devices using FCM.

Also, Is it possible that InstanceResultId class can't be imported because of the earlier version of the firebase running in my app?

I tried to upgrade the version but again it showed a bunch of errors so I kept it to 11.0.4 only.

Here is the Gradle file in app folder:

apply plugin: 'com.android.application'

android {

    compileSdkVersion 26

    defaultConfig {
        applicationId "in.w3dev.fcm2"
        minSdkVersion 19
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    }

    buildTypes {

        release {

            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}
dependencies {

    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'com.google.firebase:firebase-messaging:20.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

    implementation 'com.squareup.okhttp3:okhttp:3.10.0'


}


apply plugin: 'com.google.gms.google-services'

Upvotes: 1

Views: 1950

Answers (2)

Varsha Vaidywan
Varsha Vaidywan

Reputation: 13

UPDATE : I fixed the problem. I updated the android studio to it's newest version and alongside it, the gradle and all other things were updated and with the latest firebase version dependencies added, the code worked just fine.

Upvotes: 0

Chan Myae Aung
Chan Myae Aung

Reputation: 547

To send push notification to specific device, your backend server need to store device token and send notification to that token.

You can retrieve current device token and send to server as below.

FirebaseInstanceId.getInstance().getInstanceId()
    .addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
        @Override
        public void onComplete(@NonNull Task<InstanceIdResult> task) {
            if (!task.isSuccessful()) {
                return;
            }

            // Get new Instance ID token and send to server
            String token = task.getResult().getToken();
            sendTokenToServer(token);
        }
    });

And then, you server will you that token to send push notification to that device.

Keep in mind, you cannot use firebase console to send noti directly to specific device as it has no UI contorl for that. But you can use postman to test.

Upvotes: 1

Related Questions