FCM firebase cloud messaging Android Studio Source Code Free

FCM firebase cloud messaging Android Studio Source Code Free


Hi,
Friends Today i want to tell you how to set cloud messaging service in an Android Mobile Application, this is simple way to set Cloud Messaging in Android aap throw firebase console.

 How to Setup Project

Open android Studio and Create New Project
gave name Project  like firebasecloudnotification
now open firebase click here
sinein with gmail account and go to Console and Ad Project
FCM firebase cloud messaging Android Studio Source Code Free


 Now gave the project name (firebasecloudnotification) and select the country like Pakistan etc
Click Create Project  See this image for Help
FCM firebase cloud messaging Android Studio Source Code Free

click continue button and You see image like this and do like this image
FCM firebase cloud messaging Android Studio Source Code Free
after this you see new window in firebase console
enter your android pakage name there,
enter SHA1
now click register APP


FCM firebase cloud messaging Android Studio Source Code Free

After this shown window images
you download googleservice.json  file

paste this file in your project
watch image for more
FCM firebase cloud messaging Android Studio Source Code Free

FCM firebase cloud messaging Android Studio Source Code Free
Now go to fire-base console and click on continue button
now Copy this dependencies
classpath 'com.google.gms:google-services:3.2.0'
Now go to android studio select android instead of project
now select grade Script
select built.grade Module Aap
and paste firebase dependencies under this dependencies

dependencies {
    classpath 'com.android.tools.build:gradle:3.1.1'
classpath 'com.google.gms:google-services:3.2.0'

now select build.grade project name
past this dependcies

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


under these dependencies

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:27.1.1'    implementation 'com.android.support.constraint:constraint-layout:1.1.0'    testImplementation 'junit:junit:4.12'    androidTestImplementation 'com.android.support.test:runner:1.0.1'    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'}
apply plugin: 'com.google.gms.google-services'
now fire-core dependency
compile 'com.google.firebase:firebase-core:12.0.1'
past this dependency
there
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:27.1.1'    implementation 'com.android.support.constraint:constraint-layout:1.1.0'    testImplementation 'junit:junit:4.12'    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    compile 'com.google.firebase:firebase-core:12.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'}
apply plugin: 'com.google.gms.google-services'

Add one more dependency between these dependencies

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:27.1.1'    implementation 'com.android.support.constraint:constraint-layout:1.1.0'    testImplementation 'junit:junit:4.12'    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    compile 'com.google.firebase:firebase-core:12.0.1'
    compile 'com.google.firebase:firebase-messaging:15.0.0'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'}
goto the firebase console and finish the project

now create 2 classes
Name class one is  MyFirebaseInstanceIdservice


leave pakage name and delet other deta
in this class past this code under the pakage name


import android.util.Log;

import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.FirebaseInstanceIdService;


public class MyFirebaseInstanceIdservice extends FirebaseInstanceIdService {
    private static final String REG_TOKEN = "REG_TOKEN";
    @Override
    public void onTokenRefresh(){
        String recent_token = FirebaseInstanceId.getInstance().getToken();
        Log.d(REG_TOKEN, recent_token);
    }
}



now create Another class
gave class name  MyFirebaseMessagingService
and past this code under the pakage like previou class we do.

import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.support.v4.app.NotificationCompat; import com.google.firebase.messaging.FirebaseMessagingService; import com.google.firebase.messaging.RemoteMessage; import com.owais.earnmony.MainActivity; import com.owais.earnmony.MainMenuActivity; import com.owais.earnmony.R; import com.owais.earnmony.SignUpActivity; public class MyFirebaseMessagingService extends FirebaseMessagingService { @Override public void onMessageReceived(RemoteMessage remoteMessage) { Intent intent = new Intent(this,MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent, PendingIntent.FLAG_ONE_SHOT); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this); notificationBuilder.setContentTitle("FCM NOTIFICATION"); notificationBuilder.setContentText(remoteMessage.getNotification().getBody()); notificationBuilder.setAutoCancel(true); notificationBuilder.setSmallIcon(R.mipmap.earnmoneyicon); notificationBuilder.setContentIntent(pendingIntent); NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(0,notificationBuilder.build()); super.onMessageReceived(remoteMessage); } }
Previous
Next Post »