How to add push notifications to android app with onesignal and Firebash on Android Studio

  1. Go to Android Studio “Tool > Firebase > cloud messaging >SetUp F.C.M > Connect to firebase (it will open ) Brauser)
  2. Create Firebase Account >Cuntineu > Connect
  3. open “https://console.firebase.google.com” open your account
  4. Click setting icon > project setting > Cloud messaging
  5. Enable Cloud Messaging API (Legacy)
  6. Go to service Account > Generet new private key > Generete (It will download the Service Account JSON)

One signal 

  1. go to one signal Website “www.onesignal.com”
  2. Add new app/website > Name your project > Select google android > upload firebase Service Account JSON Continue
  3. Copy App Id

Setup app id on Android studio 

  1. Add FCM To the app
  2. Setup accourding to this url ‘https://documentation.onesignal.com/docs/android-sdk-setup”

a. pase this code on “Build.gredil (app)” of dependencies 

implementation 'com.onesignal:OneSignal:[5.0.0, 5.99.99]'

click Sync Now 

b. Past this code on MainActivity.java “down to the  private WebView mywebView;

private static final String ONESIGNAL_APP_ID = "7574f5d1-99fb-4912-bcde-2a85f3c93cc5";

c. Past this code on MainActivity.java “before the last colum of setContentView(R.layout.activity_main);

EXAMPLE

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);




Past code here 
}
 // Verbose Logging set to help debug issues, remove before releasing your app.
        OneSignal.getDebug().setLogLevel(LogLevel.VERBOSE);

        // OneSignal Initialization
        OneSignal.initWithContext(this, ONESIGNAL_APP_ID);

        // requestPermission will show the native Android notification permission prompt.
        // NOTE: It's recommended to use a OneSignal In-App Message to prompt instead.
        OneSignal.getNotifications().requestPermission(true, Continue.with(r -> {
            if (r.isSuccess()) {
                if (r.getData()) {
                    // `requestPermission` completed successfully and the user has accepted permission
                }
                else {
                    // `requestPermission` completed successfully but the user has rejected permission
                }
            }
            else {
                // `requestPermission` completed unsuccessfully, check `r.getThrowable()` for more info on the failure reason
            }
        }));

 



		
		
			

Leave a Comment

Your email address will not be published. Required fields are marked *