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'