FCM, GCM nin yeni versiyonudur ve buluta bağlı çevrim içi bildirim göndermek için kullanılır, daha sonra belki çevrim dışı olarak uygulama içindeki eylemler ile o cihaza özel bildirim oluşturmanın anlatımını da yapabilirim.
FCM Console (https://console.firebase.google.com/) Adresinden 'CREATE NEW PROJECT' yapıp;
Şeklinde yapıyoruz, fakat Türkçe karakter kullanmıyoruz;
(https://www.replikacep.com/proxy.php?request=http%3A%2F%2Fi.hizliresim.com%2FgoO68O.png&hash=1043f1b42da2319761b3f6fb8bf60da3a0ed5fdf)
Açılan yerde 'Add Firebase to Your Android App' yazan yere tıklıyoruz;
(https://www.replikacep.com/proxy.php?request=http%3A%2F%2Fi.hizliresim.com%2FEJPDrg.png&hash=2fb13719361d7ce19b9152f60b95bfa370cce945)
'Package name' yazan yere uygulamamızın paket adını yazıyoruz;
(https://www.replikacep.com/proxy.php?request=http%3A%2F%2Fi.hizliresim.com%2F5VDyal.png&hash=1cf81f619c1682b4f6b6303337a74dbc603dec4c)
'ADD APP' yazan yere tıklayınca 'google-service.json' dosyası inecektir, ardından 'Skip to the console' a tıklıyoruz;
(https://www.replikacep.com/proxy.php?request=http%3A%2F%2Fi.hizliresim.com%2FL3bDRz.png&hash=e9600689effab44ccaf35d77de7dadaeb5694554)
Ardından Uygulamamızı oluşturuyoruz.
Yoksa Android Studio yu indiriyoruz bu linkten;
Android Studio (https://developer.android.com/studio/index.html)
Yeni bir proje oluşturuyoruz ardından projenin ana dizinindeki app dizinine inen 'google-service.json' adlı dosyayı atıyoruz. (Var olan bir proje üzerinde de yapılabilir tüm bu adımlar.)
Projenin ana dizinindeki build.gradle dosyasında gerekli yere gösterdiğim satırı ekliyoruz;
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.0'
//Yalnızca altdaki satırı ekle.
classpath 'com.google.gms:google-services:3.0.0'
}
}
app dizinindeki build.gradle dosyasının en altına da bu kodu ekliyoruz;
apply plugin: 'com.google.gms.google-services'
Yine app dizinindeki build.gradle dosyasında gerekli yere gösterdiğim kodu ekliyoruz;
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:design:23.4.0'
//yalnızca altdaki satırı ekle.
compile 'com.google.firebase:firebase-messaging:9.0.0'
}
Daha sonra yeni 'IDServisi' adında yeni bir sınıf oluşturup package name satırından sonrasına bunları ekliyoruz;
import com.google.firebase.iid.*;
public class IDServisi extends FirebaseInstanceIdService {
public void onTokenRefresh(){
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
sendRegistrationToServer(refreshedToken);
}
private void sendRegistrationToServer(String token) {
}
}
Ardından 'MesajServisi' adında bir sınıf oluşturup package name satırından sonrasına bunları ekliyoruz;
import android.app.*;
import android.content.*;
import android.media.*;
import android.net.*;
import android.support.v4.app.*;
import com.google.firebase.messaging.*;
public class MesajServisi extends FirebaseMessagingService{
public void onMessageReceived(RemoteMessage remoteMessage){
sendNotification(remoteMessage.getNotification().getBody());
}
private void sendNotification(String messageBody){
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentText(messageBody)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
}
En son bu sınıfları 'AndroidManifest.xml' dosyasının en altına bu kodları ekleyip servis olarak tanımlıyoruz;
<service
android:name=".MesajServisi" >
<intent-filter>
<action
android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service
android:name=".IDServisi" >
<intent-filter>
<action
android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
Sıra geldi bildirim gönderme işine;
Console dan 'Notifications' sekmesine girip ekranda tek buton var zaten ona basıyoruz.
Ardından şeklinde ayarlar yapıyoruz;
(https://www.replikacep.com/proxy.php?request=http%3A%2F%2Fi.hizliresim.com%2Fv4j6qR.png&hash=0376b574033a298b1701cddbc72432d04caff75d)
İstersekde bu ayarlarıda 'Advanced Options' a basıp yapıyoruz;(https://www.replikacep.com/proxy.php?request=http%3A%2F%2Fi.hizliresim.com%2Fjnygkr.png&hash=9cf9c9245b7c788a47853e9eab12ae31bf95b2bb)
Sound 'Enabled' olursa bildirim giderken sesde çalar, 'Disabled' olursa da çalmaz.
Ayriyetten kodlarda bildirim sesi cihazın standart bildirim sesi olarak tanımlıdır.
Bu şekilde uygulamanın yüklü olduğu bütün cihazlara aynı anda eş zamanlı olarak bildirim atabilirsiniz.
Daha sonra bildirim atmak içinse 'Delivery date' ayarını 'Send Later' yapıyoruz.
Ayrıca console a girdiğiniz package name ve uygulamanın paket adı aynı olmalı.
Örnek;
(https://www.replikacep.com/proxy.php?request=http%3A%2F%2Fi.hizliresim.com%2F81Y6Qk.png&hash=e7b834a9317e82c91367b560126e2819f4cfdaa8) (https://www.replikacep.com/proxy.php?request=http%3A%2F%2Fi.hizliresim.com%2F3oEOOM.png&hash=a0ac7275e16e2ced3cfc9d2d879f20a03ed85c18)
Buda kendi projem, burada yazdığım kodları ekledim konuya örnek olarak direk bunu inceleyebilirsiniz ancak içinden 'google-service.json' dosyamı sildim;
https://drive.google.com/file/d/0B1I1_H5...sp=sharing (https://drive.google.com/file/d/0B1I1_H51nRR6SzdrUVBXTEQ2MG8/view?usp=sharing)