Android Development's post

post
Hello, wonderfully friends today in this blog in learning in android XML file creating different type shape creating. A shape is an XML file that defines a geometric shape, including strokes, colors, and gradients


First Creating .xml file :

  • 1.Open android studio
  • 2.Go to drawable 
  • 3.right click drawable menu
  •  flow below image steps:


Now friend starts coding :


  • -Creating a capsule shape flow below code:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >

  <corners
    android:bottomLeftRadius="30dp"
    android:bottomRightRadius="30dp"
    android:radius="60dp"
    android:topLeftRadius="30dp"
    android:topRightRadius="30dp" />

  <solid android:color="#CFCFCF" />

  <padding
    android:bottom="0dp"
    android:left="0dp"
    android:right="0dp"
    android:top="0dp" />

  <size
    android:height="60dp"
    android:width="270dp" />
 
</shape>


Corners:

  • The below image is showing the shape corner radius
Solid :

  • -Solid means shape inner color “#CFCFCF”

Padding:

- There is always confusion developer between padding and margin. Both provide extra space/gap inside or outside the container. In simple words, margin means to push outside, whereas padding means to push inside.

Size:

  •  Its very simple shape width - height 
October 07, 20201 minuteauthorPankaj Valani
post
Today I’m going to write on Create Floating Widget never stop closing the app from the background. First of all, you must know what is the service in Android.

What is a service in Android?

A service is a component that runs in the background without direct interaction with the user. Since the service has no user interface, it is not bound to the life cycle of the activity. If you want to repeat a task, using the service is the best way.
Service stops are one of the biggest problems when you clear the application from background functions.


My service is below:

public class FloatingViewService extends Service  {

    private WindowManager mWindowManager;
    private View mFloatingView;
    private View floatingButton;
    private final static float CLICK_DRAG_TOLERANCE = 10;

    private float downRawX, downRawY;
    private float dX, dY;
    int LAYOUT_FLAG;
    private SpeechRecognizer speechRecognizer;

    public FloatingViewService() {
    }
		Context context;

    public FloatingViewService(Context applicationContext) {
        super();
        context = applicationContext;
        Log.i("HERE", "here service created!");
    }
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
					 KeyguardManager myKM = (KeyguardManager) this.getSystemService(Context.KEYGUARD_SERVICE);
              if( myKM.inKeyguardRestrictedInputMode()) {
                  Intent i = new Intent(this,FloatingViewService.class);
                  startService(i);
//            //it is locked
                } else {
                    Intent i = new Intent(this, FloatingViewService.class);
                    startService(i);
        //it is not locked
             }
            return START_STICKY;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mFloatingView = LayoutInflater.from(this).inflate(R.layout.floating_layout, null);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        } else {
            LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_PHONE;
        }

        final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                LAYOUT_FLAG,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT);

        mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
        mWindowManager.addView(mFloatingView, params);

        floatingButton.setOnTouchListener(new View.OnTouchListener() {
            private int initialX;
            private int initialY;
            private float initialTouchX;
            private float initialTouchY;

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        initialX = params.x;
                        initialY = params.y;
                        initialTouchX = event.getRawX();
                        initialTouchY = event.getRawY();

                        downRawX = event.getRawX();
                        downRawY = event.getRawY();
                        dX = v.getX() - downRawX;
                        dY = v.getY() - downRawY;

                        return true;

                    case MotionEvent.ACTION_UP:

                        float upRawX = event.getRawX();
                        float upRawY = event.getRawY();

                        float upDX = upRawX - downRawX;
                        float upDY = upRawY - downRawY;
                        return true;

                    case MotionEvent.ACTION_MOVE:
                        params.x = initialX + (int) (event.getRawX() - initialTouchX);
                        params.y = initialY + (int) (event.getRawY() - initialTouchY);
                        mWindowManager.updateViewLayout(mFloatingView, params);
                        return true;
                }
                return false;
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
AndroidManifest.xml  file and update your service declarations under the application tag. Also, add  FOREGROUND_SERVICE, INTERNET and  WAKE_LOCK .Make sure you don't forget to include them as they won't work otherwise.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
          package="com.example.endless">

    <uses-permission android:name="android.permission.FOREGROUND_SERVICE"></uses-permission>
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <uses-permission android:name="android.permission.WAKE_LOCK" />

    <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">

        <service
                android:name=".FloatingViewService"
                android:enabled="true"
                android:exported="false">
        </service>

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>
</manifest>

You can use the following code to check if the service is running. If the service is already in running mode, it does not need to be restarted otherwise start in your MainActivity.

FloatingViewService mFloatingService = new FloatingViewService(getApplicationContext());
  Intent mServiceIntent = new Intent(getApplicationContext(), mFloatingService.getClass());
if (!isMyServiceRunning(mFloatingService.getClass())) {
    startService(mServiceIntent);
}

Method isMyServiceRunning() is below add this in your MainActivity.

private boolean isMyServiceRunning(Class<?> serviceClass) {
    ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
        if (serviceClass.getName().equals(service.service.getClassName())) {
            Log.i ("isMyServiceRunning?", true+"");
            return true;
        }
    }
    Log.i ("isMyServiceRunning?", false+"");
    return false;
}
Then Create a Floating Widget design. Create File floating_layout.xml Like This

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <RelativeLayout
        android:id="@+id/rl_close_button"
        android:layout_width="100dp"
        android:layout_height="wrap_content">

        <RelativeLayout
            android:id="@+id/relativeLayoutParent"
            android:layout_width="80dp"
            android:layout_height="70dp">

            <RelativeLayout
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:background="@drawable/floating_round_main_shape">

                <RelativeLayout
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    android:layout_centerInParent="true"
                    android:background="@drawable/floating_inner_round_shape">

                    <ImageView
                        android:layout_width="25dp"
                        android:layout_height="25dp"
                        android:layout_centerInParent="true"
                        android:src="@drawable/ic_voice_icon" />

                </RelativeLayout>

            </RelativeLayout>

        </RelativeLayout>

    </RelativeLayout>

</RelativeLayout>

I hope it will help these services. 
Note:-This service will run better in Oreo and above versions.


October 04, 20203 minutesauthorVivek Beladiya
post
First, create a new Android Studio project and add the dependencies and plugin. First set up OneSignal in your project.
Add OneSignal dependency and plugin to your build.Gradle app-level.

apply plugin: 'com.onesignal.androidsdk.onesignal-gradle-plugin'

dependencies {
    implementation 'com.onesignal:OneSignal:3.12.6'
}
end add OneSignal dependency to your build.Gradle project-level.
buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:4.1.0'
        classpath 'gradle.plugin.com.onesignal:onesignal-gradle-plugin:[0.12.4, 0.99.99]'
    }
}
Step: 2  Create a new OneSignal Project

  • login your OneSignal Account than create an app
    1.png 34.06 KB
  • Enter Your Android Application Name & Select platform  Google Android(FCM)
    2.png 76.02 KB
  • Generate a Firebase Server Key Read the documentation.
  •  Enter your Firebase Server Key & Firebase Sender ID.

Step: 3  Add Your Created Onesignal App ID

Add OneSignl default config  to your build.Gradle app-level.
android {
    compileSdkVersion 30
    buildToolsVersion "30.0.2"

    defaultConfig {
        applicationId "com.myapplication.app"
        minSdkVersion 17
        targetSdkVersion 30
        versionCode 1
        versionName "1.0.0"
        multiDexEnabled true
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        manifestPlaceholders = [onesignal_app_id               : "Enter Your App ID",
                                onesignal_google_project_number: " Enter Firebase Sender ID"]
    }

    buildTypes {
        release {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}
The next step is to create the class MyApplication & Declare the class MyApplication in AndroidManifest.xml.

import android.app.Application;
import android.content.Context;
import androidx.multidex.MultiDex;
import com.onesignal.OneSignal;

public class MyApplication extends Application {
    public static Context context;

    @Override
    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();
        context = this;
				
        OneSignal.startInit(this)
                .inFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification)
                .unsubscribeWhenNotificationsAreDisabled(true)
                .init();
    }

    public static Context getContext() {
        return context;
    }
}
Declare the class MyApplication in AndroidManifest.xml.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.myapplication.app">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <application
        android:name="com.myapplication.app.MyApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:requestLegacyExternalStorage="true"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity
            android:name="com.myapplication.app.activity.SplashActivity"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


October 01, 20201 minuteauthorVivek Beladiya
post
What is a recycler view?

  • RecyclerView is used to show the data in the form of a scrollable list. It is a ViewGroup to display a large set of data. For each item in a large dataset, it displays a View. So RecyclerView is very useful no use the scrollable list.

Why use RecyclerView in Android?

  • One reason is so we can create a List through the LinearLayout and the orientation can be vertical. For example, in the Facebook application, all parameters will be the same as share, comment, like, etc. Now if we will take LinearLayout, all the views will create separate views in the memory.
Add dependencies :



dependencies {  

    implementation "androidx.recyclerview:recyclerview:1.1.0"
} 

Notes:

  • Androidx in recycler view dependencies:  implementation "androidx.recyclerview:recyclerview:1.1.0"
  • Your android studio, not androidx:  implementation 'com.android.support:recyclerview-v7:28.0.0'

Now we will add the RecyclerView layout in our activity_main.xml file

<?xml version="1.0" encoding="utf-8"?>  
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
xmlns:app="http://schemas.android.com/apk/res-auto"  
xmlns:tools="http://schemas.android.com/tools"  
android:layout_width="match_parent"  
android:layout_height="match_parent"  
tools:context=".MainActivity">  
  
<androidx.recyclerview.widget.RecyclerView  
    android:id="@+id/recyclerView"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent" />  
  
</RelativeLayout>

We create a layout file named row_layout.xml.

<?xml version="1.0" encoding="utf-8"?>  
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
xmlns:app="http://schemas.android.com/apk/res-auto"  
xmlns:tools="http://schemas.android.com/tools"  
android:layout_width="match_parent"  
android:layout_height="match_parent"  
tools:context=".MainActivity">  
  
<androidx.recyclerview.widget.RecyclerView  
    android:id="@+id/recyclerView"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent" />  
  
</RelativeLayout>

We create a layout file named row_layout.xml

<?xml version="1.0" encoding="utf-8"?>  
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:orientation="horizontal">  
  
    <ImageView  
        android:id="@+id/image"  
        android:layout_width="300px"  
        android:layout_height="300px"  
        android:layout_marginLeft="10dp"  
        android:layout_marginTop="10dp"  
        android:src="@color/colorPrimaryDark" />  
  
    <TextView  
        android:id="@+id/description"  
        android:layout_width="250dp"  
        android:layout_height="55dp"  
        android:layout_toRightOf="@id/image"  
        android:layout_marginLeft="15dp"  
        android:layout_marginTop="35dp"  
        android:text="abcd"  
        android:textColor="#000"  
        android:textSize="30sp" />  
  
</RelativeLayout>

Create a Model Class :

public class Data {  
  
    public String name;  
  
    public String getName() {  
        return name;  
    }  
  
    public void setName(String name) {  
        this.name = name;  
    }  
  
    public int getImageId() {  
        return imageId;  
    }  
  
    public void setImageId(int imageId) {  
        this.imageId = imageId;  
    }  
  
    public int imageId;  
  
    Data(String name, int imageId) {  
        this.name = name;  
        this.imageId = imageId;  
    }  
} 

Create RecyclerVIew_Adapter.java

public class RecyclerView_Adapter extends RecyclerView.Adapter<View_Holder> {  
  
    List<Data> list = Collections.emptyList();  
    Context context;  
  
    public RecyclerView_Adapter(List<Data> data, Application application) {  
        this.list = data;  
        this.context = application;  
    }  
  
    @NonNull  
    @Override  
    public View_Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {  
        //Inflate the layout, initialize the View Holder  
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_layout, parent, false);  
        View_Holder holder = new View_Holder(v);  
        return holder;  
    }  
  
    @Override  
    public void onBindViewHolder(@NonNull View_Holder holder, int position) {  
  
        //Use the provided View Holder on the onCreateViewHolder method to populate the current row on the RecyclerView  
        holder.name.setText(list.get(position).name);  
        holder.imageView.setImageResource(list.get(position).imageId);  
  
    }  
  
    @Override  
    public int getItemCount() {  
        return list.size();  
    }  
} 

  • onCreateViewHolder()- It inflates the row layout and initializes the view holder. It handles findViewById) (methods, find views once, and recycle them so that repetitive calls are avoided.
  • onBindViewHolder()- It uses the onCreateViewHolder() View Holder to fill in the current RecyclerView row with data.
  • getItemCount()- This method returns the collection size which contains the items that we wish to show.
The code file of the ViewHolder Java class is listed below.

import android.view.View;  
import android.widget.ImageView;  
import android.widget.TextView;  
import androidx.recyclerview.widget.RecyclerView;  
  
public class View_Holder extends RecyclerView.ViewHolder {  
  
    TextView name;  
    ImageView imageView;  
  
    View_Holder(View itemView) {  
        super(itemView);  
        name = (TextView) itemView.findViewById(R.id.name);  
        imageView = (ImageView) itemView.findViewById(R.id.image);  
    }  
} 

MainActivity.java class file:

 public class MainActivity extends AppCompatActivity {  
  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
  
        List<Data> data = fill_with_data();  
  
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);  
        RecyclerView_Adapter adapter = new RecyclerView_Adapter(data, getApplication());  
        recyclerView.setAdapter(adapter);  
        recyclerView.setLayoutManager(new LinearLayoutManager(this));  
  
    }  
  
    public List<Data> fill_with_data() {  
  
        List<Data> data = new ArrayList<>();  
        data.add(new Data("Android", R.drawable.c));  
        data.add(new Data("Kotlin", R.drawable.cc));  
		data.add(new Data("Kotlin", R.drawable.ccc));  
        return data; 
    }  
} 


September 22, 20203 minutesauthorPankaj Valani
post
  • Developer use API calling retrofit, volley JSON or other tools but API response check-in postman time confusion how to creating this response model class
  • So a friend in this topic I will try your confusion remove flow below all steps and 5 min inner creating a wonderful model class
Step: 1 Android studio in install "RoboPOJOGenerator"

  • Location in the android studio:
  1. Open android studio
  2. Go to File
  3. Go to Setting
  4. Click plugin
  • check the below image && click and install RoboPOJOGenerator then your android studio restart
Step: 2 How to open RoboPOJOGenerator

  1. Open android studio
  2. Go to File
  3. Select directory below the image in showing related select any one directory
  4. Right-click - select New - then below showing "RoboPOJOGenerator"
Step: 3 How to add postman response

  1. Postman response copy
  2. Paste this response in "RoboPOJOGenerator" in one black color dialog showing in android studio
  3. Left top side one cursor visible on this area in paste this response
  4. Select "GSON" option in "RoboPOJOGenerator"
  5. Last click generate button after a few moments your model class is ready
August 15, 20201 minuteauthorPankaj Valani
post
Facebook Login With Firebase In Android Java

1.Add this library build.gradle(:app)

  • This library user for firebase integration dependencies 
  • {
    implementation 'com.google.firebase:firebase-auth:19.4.0'
    }
2.Add this permission in AndroidManifest.xml

  • This permission use internet connection checking
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
3.Below Facebook Button Add on activity xml :

<com.facebook.login.widget.LoginButton
         android:id="@+id/login_button"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:layout_gravity="center"
         android:visibility="gone" />
4.Activity in add this permission

  • This permission user for user data get. Without this permission user data not meet

  • loginButton” This button is Facebook button 

  • LoginManager.getInstance().logInWithReadPermissions(FacebookLoginActivity.this, Arrays.asList("email", "public_profile")); 
  • loginButton.setReadPermissions("email", "public_profile");
5.Initialize Authentication:

void initializeAuthentication(){
                FirebaseAuth  mAuth = FirebaseAuth.getInstance();
             GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
             .requestIdToken(getString(R.string.default_web_client_id))
             .requestEmail()
             .build();
    mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
}
6.Add This Function:

  • This function user login result return
void faceBookLogin(LoginResult loginResult){
     setFacebookData(loginResult);
     Profile profile = Profile.getCurrentProfile();
     if (profile != null) {
       String avatar =  ImageRequest.getProfilePictureUri(profile.getId(), 200, 200).toString();
   }
   handleFacebookAccessToken(loginResult.getAccessToken());
}
7.Handle Token :

  • This token return firebase login fails or not an event
private void handleFacebookAccessToken(AccessToken token) {
AuthCredential credential = FacebookAuthProvider.getCredential(token.getToken());
    mAuth.signInWithCredential(credential)
           .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
               @Override
               public void onComplete(@NonNull Task<AuthResult> task) {
                   if (task.isSuccessful()) {
                       FirebaseUser user = mAuth.getCurrentUser();
                       user.getIdToken(true).addOnCompleteListener(new OnCompleteListener<GetTokenResult>() {
                           @Override
                           public void onComplete(@NonNull Task<GetTokenResult> task) {
                               String token = task.getResult().getToken();

                           }
                       });

                   } else {
                      String errorCode = String.valueOf(task.getException());
                       Toast.makeText(FacebookLoginActivity.this, "Login failed.", Toast.LENGTH_SHORT).show();
                    } 
            }
    });
}
Notes:

  • “Default_web_client_id” this keyword, not changes because creating google-services.json file time automatically this keyword in add values so this key work put at its
How to creating google JSON file : JSON File

Add google JSON file this location in android studio:

July 28, 20201 minuteauthorPankaj Valani
post

Firebase Cloud Messaging (FCM) is a cross-platform messaging solution that lets you deliver messages for free.

It allows you to send push notifications from the Firebase console or from the application server or some trusted server where logic runs.

Step 1:- Create a new Android Studio Project

First, create a new Android Studio project and add the dependencies. First set up Firebase in your project. You can find a good tutorial.

Add Firebase messaging dependency to your app-level.

build.Gradle
dependencies {
  implementation 'com.google.firebase:firebase-messaging:19.0.1'
}

Step 2: Create a Firebase Service

The next step is to create Firebase Services:- MyFirebaseInstanceIDService and MyFirebaseMessagingService. first MyFirebaseInstanceIDService service will handle the device registration process and the second MyFirebaseInstanceIDService will handle the reception and display of notifications. The services have no visual interface and are used for operations that run in the background. To create a service, right-click and select the Applications folder New -> Service -> Service.

setting

Type in your service name, and click the Finish button. Repeat the same steps for the second service.

setting

AndroidManifest.xml file and update your service declarations under the application tag. Also, add INTERNET and CLOUD TO DEVICE MESSAGING permissions so your app can interact with the FCM server.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.exmple.notify">

  <uses-permission android:name="android.permission.INTERNET" />
  <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

  <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>

    <service android:name=".MyFirebaseInstanceIDService">
      <intent-filter>
        <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
      </intent-filter>
    </service>

    <service
      android:name=".MyFirebaseMessagingService"
      android:permission="com.google.android.c2dm.permission.SEND">
      <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
        <action android:name="com.google.android.c2dm.intent.RECEIVE" />
      </intent-filter>
    </service>
  </application>
</manifest>

To handle the device registration process, MyFirebaseInstanceIDService must increase the FireBaseInstenside service class. Under this service, override the tokenrefresh() method so that whenever the system decides to refresh the tokens, it will be requested. This usually happens when the user installs/reinstalls the application or when the user clears the application data.

Since you are sending notifications between devices, each user must subscribe to an issue with a different user_id. This ensures that users receive notifications sent to topics that match their user_id. Here is the implementation of MyFirebaseInstanceIDSericiclass.

public class MyFirebaseMessagingService extends FirebaseMessagingService {

  private final String ADMIN_CHANNEL_ID = "admin_channel";

  @Override
  public void onMessageReceived(RemoteMessage remoteMessage) {
    final Intent intent = new Intent(this, MainActivity.class);
    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    int notificationID = new Random().nextInt(3000);
   /*
    Apps targeting SDK 26 or above (Android O) must
    implement notification channels and add their notifications to at least one of them. Therefore, confirm if the version is Oreo or higher, then setup notification channel
   */
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
      setupChannels(notificationManager);
    }

    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
    Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.drawable.notify_icon);
    Uri notificationSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIF CATION);

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, ADMIN_CHANNEL_ID)
        .setSmallIcon(R.drawable.notify_icon)
        .setLargeIcon(largeIcon)
        .setContentTitle(remoteMessage.getData().get("title"))
        .setContentText(remoteMessage.getData().get("message"))
        .setAutoCancel(true)
        .setSound(notificationSoundUri)
        .setContentIntent(pendingIntent);

    // Set notification color to match your app color template
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
      notificationBuilder.setColor(getResources().getColor(R.color.colorPrimaryDark));
    }
    notificationManager.notify(notificationID, notificationBuilder.build());
  }

  @RequiresApi(api = Build.VERSION_CODES.O)
  private void setupChannels(NotificationManager notificationManager) {
    CharSequence adminChannelName = "New notification";
    String adminChannelDescription = "Device to device notification ";

    NotificationChannel adminChannel;
    adminChannel = new NotificationChannel(ADMIN_CHANNEL_ID, adminChannelName, NotificationManager.IMPORTANCE_HIGH);
    adminChannel.setDescription(adminChannelDescription);
    adminChannel.enableLights(true);
    adminChannel.setLightColor(Color.RED);
    adminChannel.enableVibration(true);

    if (notificationManager != null) {
      notificationManager.createNotificationChannel(adminChannel);
    }
  }
}

Step 4: Implement the notification sending logic

This is the most important part of the whole article. This is where you define the content of the instruction and how it will be modeled. However, before you dive into coding, follow these steps to get your server key from the Firebase console.

console

Navigate to the Cloud Messaging tab, and copy your Server key

console

Implement the Sending Logic: An FCM server with the following request properties only needs an HTTP post request to send a push notification:

Method Type: POST

URL: https://fcm.googleapis.com/fcm/send

Headers:

Authorization: key="Firebase server key" Content-Type: application/json

Body:

{
 "to": "/topics/notification_userId",
 "data": {
  "title": "Notification title",
  "message": "Notification message",
  "key1" : "value1",
  "key2" : "value2" //additional data you want to pass
 }
}

With these concepts in mind, you will first create a JsonObject of Notification body within your activity class. This object budget will contain the subject of the receiver, the title of the notification, the notification message, and the other key/value pair you want to add.

public class MainActivity extends AppCompatActivity {
  EditText edtTitle;
  EditText edtMessage;
  final private String FCM_API = "https://fcm.googleapis.com/fcm/send";
  final private String serverKey = "key=" + "Your Firebase server key";
  final private String contentType = "application/json";
  final String TAG = "NOTIFICATION TAG";

  String NOTIFICATION_TITLE;
  String NOTIFICATION_MESSAGE;
  String TOPIC;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    edtTitle = findViewById(R.id.edtTitle);
    edtMessage = findViewById(R.id.edtMessage);
    Button btnSend = findViewById(R.id.btnSend);

    btnSend.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        TOPIC = "/topics/userABC"; //topic must match with what the receiver subscribed to
        NOTIFICATION_TITLE = edtTitle.getText().toString();
        NOTIFICATION_MESSAGE = edtMessage.getText().toString();

        JSONObject notification = new JSONObject();
        JSONObject notifcationBody = new JSONObject();
        try {
          notifcationBody.put("title", NOTIFICATION_TITLE);
          notifcationBody.put("message", NOTIFICATION_MESSAGE);
          notification.put("to", TOPIC);
          notification.put("data", notifcationBody);
        } catch (JSONException e) {
          Log.e(TAG, "onCreate: " + e.getMessage() );
        }
        sendNotification(notification);
      }
    });
  }
  private void sendNotification(JSONObject notification) {
 ...
  }
}

The next step is to request the network server using the library volley, then use the parameters to the root server will request notification on the target device.

public class MainActivity extends AppCompatActivity {

  ....

  private void sendNotification(JSONObject notification) {
    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(FCM_API, notification,
            new Response.Listener<JSONObject>() {
              @Override
              public void onResponse(JSONObject response)
              {
                Log.i(TAG, "onResponse: " + response.toString());
                edtTitle.setText("");
                edtMessage.setText("");
              }
            },
            new Response.ErrorListener() {
              @Override
              public void onErrorResponse(VolleyError error) {
                Toast.makeText(MainActivity.this, "Request error", Toast.LENGTH_LONG).show();
                Log.i(TAG, "onErrorResponse: Didn't work");
              }
            }){
          @Override
          public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String> params = new HashMap<>();
            params.put("Authorization", serverKey);
            params.put("Content-Type", contentType);
            return params;
          }
        };
    MySingleton.getInstance(getApplicationContext()).addToRequestQueue(jsonObjectRequest);
  }
}

Finally, add a MySingleton class that will serve as a request queue for instruction requests.

public class MySingleton {
  private static MySingleton instance;
  private RequestQueue requestQueue;
  private Context ctx;

  private MySingleton(Context context) {
    ctx = context;
    requestQueue = getRequestQueue();
  }
  public static synchronized MySingleton getInstance(Context context) {
    if (instance == null) {
      instance = new MySingleton(context);
    }
    return instance;
  }
  public RequestQueue getRequestQueue() {
    if (requestQueue == null) {
      // getApplicationContext() is key, it keeps you from leaking the
      // Activity or BroadcastReceiver if someone passes one in.
      requestQueue = Volley.newRequestQueue(ctx.getApplicationContext());
    }
    return requestQueue;
  }
  public <T> void addToRequestQueue(Request<T> req) {
    getRequestQueue().add(req);
  }
}

With it, you're done creating your app. You can start sending push notifications between devices without typing any server-side code. Always make sure that the notification will not be delivered to you if the subject of the recipient is correct. If you do everything right, you will get the same result.

result

July 21, 20205 MinutesauthorVivek Beladiya