Sending Firebase Push Notifications - Android Device to Device
<uses-permission android:name="android.permission.INTERNET"/>
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
dependencies {
implementation 'com.github.uguraltinsoy:FCMSend:1.0.5'
}
- Server Key = Firebase Console -> Select Project -> Project settings -> Cloud Messaging -> Copy Server Key
// JAVA
public class MainActivity extends AppCompatActivity {
private static String serverKey = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// FCMSend Initialization
FCMSend.SetServerKey(serverKey);
}
}
// KOTLIN
class KotlinMainActivity : AppCompatActivity() {
private val serverKey = ""
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_kotlin_main)
// FCMSend Initialization
FCMSend.SetServerKey(serverKey)
}
}
Get device Token
// JAVA
FirebaseMessaging.getInstance().getToken()
.addOnCompleteListener(new OnCompleteListener<String>() {
@Override
public void onComplete(@NonNull Task<String> task) {
if (!task.isSuccessful()) { return; }
String token = task.getResult();
}
});
// KOTLIN
FirebaseMessaging.getInstance().token
.addOnCompleteListener(OnCompleteListener { task ->
if (!task.isSuccessful) {
return@OnCompleteListener
}
val token = task.result })
Subscribe Topic
// JAVA
FirebaseMessaging.getInstance().subscribeToTopic("<Topic Name>")
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
System.out.println("Subscription successful");
}
});
// KOTLIN
FirebaseMessaging.getInstance().subscribeToTopic("<Topic Name>")
.addOnSuccessListener {
println("Subscription successful")
}
// JAVA
HashMap<String, String> data = new HashMap<>();
data.put("key1", "data 1");
data.put("key2", "data 2");
data.put("key3", "data 3");
FCMSend.Builder build = new FCMSend.Builder("<To Device Token>")
.setTitle("<Title>")
.setBody("<Message>")
.setImage("<Image Url>") // Optional
.setClickAaction("<Click Aaction>") // Optional
.setData(data); // Optional
build.send();
// KOTLIN
val data = HashMap<String, String>()
data["key1"] = "data 1"
data["key2"] = "data 2"
data["key3"] = "data 3"
val build = FCMSend.Builder("<To Device Token>")
.setTitle("<Title>")
.setBody("<Message>")
.setImage("<Image Url>") // Optional
.setClickAaction("<Click Aaction>") // Optional
.setData(data) // Optional
build.send()
OR
// JAVA
HashMap<String, String> data = new HashMap<>();
data.put("key1", "data 1");
data.put("key2", "data 2");
data.put("key3", "data 3");
FCMSend.Builder build = new FCMSend.Builder("<To Device Token>")
.setTitle("<Title>")
.setBody("<Message>")
.setImage("<Image Url>") // Optional
.setClickAaction("<Click Aaction>") // Optional
.setData(data); // Optional
String result = build.send().Result();
// KOTLIN
val data = HashMap<String, String>()
data["key1"] = "data 1"
data["key2"] = "data 2"
data["key3"] = "data 3"
val build = FCMSend.Builder("<To Device Token>")
.setTitle("<Title>")
.setBody("<Message>")
.setImage("<Image Url>") // Optional
.setClickAaction("<Click Aaction>") // Optional
.setData(data) // Optional
val result = build.send().Result()
// JAVA
HashMap<String, String> data = new HashMap<>();
data.put("key1", "data 1");
data.put("key2", "data 2");
data.put("key3", "data 3");
FCMSend.Builder build = new FCMSend.Builder("<Topic Name>", true)
.setTitle("<Title>")
.setBody("<Message>")
.setImage("<Image Url>") // Optional
.setClickAaction("<Click Aaction>") // Optional
.setData(data); // Optional
build.send();
// KOTLIN
val data = HashMap<String, String>()
data["key1"] = "data 1"
data["key2"] = "data 2"
data["key3"] = "data 3"
val build = FCMSend.Builder("<Topic Name>", true)
.setTitle("<Title>")
.setBody("<Message>")
.setImage("<Image Url>") // Optional
.setClickAaction("<Click Aaction>") // Optional
.setData(data) // Optional
build.send()
OR
// JAVA
HashMap<String, String> data = new HashMap<>();
data.put("key1", "data 1");
data.put("key2", "data 2");
data.put("key3", "data 3");
FCMSend.Builder build = new FCMSend.Builder("<Topic Name>", true)
.setTitle("<Title>")
.setBody("<Message>")
.setImage("<Image Url>") // Optional
.setClickAaction("<Click Aaction>") // Optional
.setData(data); // Optional
String result = build.send().Result();
// KOTLIN
val data = HashMap<String, String>()
data["key1"] = "data 1"
data["key2"] = "data 2"
data["key3"] = "data 3"
val build = FCMSend.Builder("<Topic Name>", true)
.setTitle("<Title>")
.setBody("<Message>")
.setImage("<Image Url>") // Optional
.setClickAaction("<Click Aaction>") // Optional
.setData(data) // Optional
val result = build.send().Result()
<application>
<activity android:name=".MainActivity" android:exported="false">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<!-- Add -->
<intent-filter>
<action android:name="com.deeplabstudio.fcm_NOTIFICATION" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<!-- -->
</activity>
<service android:name=".PushNotificationService" android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
</application>
MainActivity
// JAVA
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
String url = bundle.getString("url");
}
}
}
// KOTLIN
class KotlinMainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) setContentView (R.layout.activity_kotlin_main)
val bundle = intent.extras
if (bundle != null) {
val url = bundle.getString("url")
}
}
}
Uğur Altınsoy
BTC : 1N7V3wX4xvGfwgBP1zQrcMSxohKKfiDxyH
ETH : 0x0df6da87e219fb4854e933f1071ad91d17afa517
XRP : rEb8TK3gBgk5auZkwc6sHnwrGVJH8DuaLh
DOGE : DKKmSHAa8GhAE5HNjmCXzkXPKTjpybY3mq
DENT : 0x0df6da87e219fb4854e933f1071ad91d17afa517