Wysyłanie powiadomienia push przy użyciu FCM
Google przestarzałe Google Cloud Messaging (GCM) i uruchomiła nowy serwer powiadomień push, który jest Firebase Cloud Messaging (FCM). FCM jest taka sama jak GCM, FCM jest wieloplatformowym rozwiązaniem komunikacyjnym dla platform mobilnych
Firebase Cloud Messaging może wysłać trzy rodzaje komunikatów (Message types)
1.Notification wiadomość
2.Data Wiadomość
3.message zarówno notyfikacji i dane
Firebase Cloud Messaging Integracja kroki: -
1.USTAW.ODBIÓR Nowy projekt lub projekt importu w Firbase Console (https://firebase.google.com/)
2.Dodaj zdjęcia tego samego pakietu Nazwa Aplikacja w aplikacji Firebase.
3. Pobierz plik "google-services.json" i umieść go w folderze aplikacji projektu. Ten plik zawiera wszystkie adresy URL i klucze usługi Google, więc nie zmieniaj ani nie edytuj tego pliku.
4. Dodaj nowe zależności Gradle w Project for Firebase.
//app/build.gradle
dependencies {
compile 'com.google.firebase:firebase-messaging:9.6.0'
}
apply plugin: 'com.google.gms.google-services'
5. Utwórz klasę zawierającą wszystkie wartości stałe, których używamy w aplikacji dla FCM.
public class Config {
public static final String TOPIC_GLOBAL = "global";
// broadcast receiver intent filters
public static final String REGISTRATION_COMPLETE = "registrationComplete";
public static final String PUSH_NOTIFICATION = "pushNotification";
// id to handle the notification in the notification tray
public static final int NOTIFICATION_ID = 100;
public static final int NOTIFICATION_ID_BIG_IMAGE = 101;
public static final String SHARED_PREF = "ah_firebase";
}
6. utworzyć klasę o nazwie MyFirebaseInstanceIDService.java które będą odbiera Firebase identyfikator rejestracji, który będzie unikalny dla każdej aplikacji. Identyfikator rejestracji służy do wysyłania wiadomości do jednego urządzenia.
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
private static final String TAG = MyFirebaseInstanceIDService.class.getSimpleName();
@Override
public void onTokenRefresh() {
super.onTokenRefresh();
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
// Saving reg id to shared preferences
storeRegIdInPref(refreshedToken);
// sending reg id to your server
sendRegistrationToServer(refreshedToken);
// Notify UI that registration has completed, so the progress indicator can be hidden.
Intent registrationComplete = new Intent(Config.REGISTRATION_COMPLETE);
registrationComplete.putExtra("token", refreshedToken);
LocalBroadcastManager.getInstance(this).sendBroadcast(registrationComplete);
}
private void sendRegistrationToServer(final String token) {
// sending gcm token to server
Log.e(TAG, "sendRegistrationToServer: " + token);
}
private void storeRegIdInPref(String token) {
SharedPreferences pref = getApplicationContext().getSharedPreferences(Config.SHARED_PREF, 0);
SharedPreferences.Editor editor = pref.edit();
editor.putString("regId", token);
editor.commit();
}
}
7.Create jedna klasa usługa o nazwie MyFirebaseMessagingService.java. To spowoduje otrzymanie wiadomości z bazy FireFase.
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = MyFirebaseMessagingService.class.getSimpleName();
private NotificationUtils notificationUtils;
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.e(TAG, "From: " + remoteMessage.getFrom());
if (remoteMessage == null)
return;
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.e(TAG, "Notification Body: " + remoteMessage.getNotification().getBody());
handleNotification(remoteMessage.getNotification().getBody());
}
}
private void handleNotification(String message) {
if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) {
// app is in foreground, broadcast the push message
Intent pushNotification = new Intent(Config.PUSH_NOTIFICATION);
pushNotification.putExtra("message", message);
LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification);
// play notification sound
NotificationUtils notificationUtils = new NotificationUtils(getApplicationContext());
notificationUtils.playNotificationSound();
}else{
// If the app is in background, firebase itself handles the notification
}
}
/**
* Showing notification with text only
*/
private void showNotificationMessage(Context context, String title, String message, String timeStamp, Intent intent) {
notificationUtils = new NotificationUtils(context);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
notificationUtils.showNotificationMessage(title, message, timeStamp, intent);
}
/**
* Showing notification with text and image
*/
private void showNotificationMessageWithBigImage(Context context, String title, String message, String timeStamp, Intent intent, String imageUrl) {
notificationUtils = new NotificationUtils(context);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
notificationUtils.showNotificationMessage(title, message, timeStamp, intent, imageUrl);
}
}
8.In na AndroidManifest.xml dodać te dwie usługi Firebase MyFirebaseMessagingService i MyFirebaseInstanceIDService.
<!-- Firebase Notifications -->
<service android:name=".service.MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service android:name=".service.MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
<!-- ./Firebase Notifications -->
Teraz wystarczy Send your First Message
Uwagi:
* 1.Read Google Doc do Firebase Cloud Messaging *
2.Jeżeli chcesz migrować Aplikacja klienta GCM dla systemu Android na platformę Fir ebase Cloud Messaging następujące kroki i DOC (Migrate a GCM Client App)
3.Android samouczek Sample i kod (Receive Reengagement Notifications)
C2DM jest przestarzała. Proszę użyć https://developer.android.com/guide/google/gcm/index.html – gigadot
ok, spróbuję nauczyć się i rozwijać powyższy tutorial – user1676640
wygląd mojej odpowiedzi tutaj: mam nadzieję, że pomoże: http: // stackoverflow. com/a/12437549/554740 – HelmiB