How to auto-run React-Native App on device starup - react-native

How to start a react-native app on device startup on Android. I have an Android app developed on react-native and if the app is running and user restarts his device, I wish to auto start my app.

Try using the react-native-autostart npm package.

I created a new .java file named BootReceiver.java:
package com.packageName;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class BootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent myIntent = new Intent(context, MainActivity.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myIntent);
}
}
then I edited the AndroidManifest.xml:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name="com.packageName.BootReceiver" android:enabled="true" android:exported="true">
<intent-filter >
<category android:name="android.intent.category.DEFAULT"/>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>

Related

App Crashes on Splash Screen on Android 7 React Native

I'm facing the splash screen issue on Android 7 and lower.
It works fine on Android 7+
but on Android 7 and lower the app crashes as soon as it is opened, without a splash screen app is working fine. Any Solutions?
I have tried using different pngs and styles and color values but still the same issue.
MainActivity.java
#Override
protected void onCreate(Bundle savedInstanceState)
{
SplashScreen.show(this, true);
super.onCreate(savedInstanceState);
}
SplashActivity.java
public class SplashActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
}
}
AndroidManifest.xml
....
<activity
android:name=".SplashActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize|uiMode"
android:launchMode="singleTask"
android:windowSoftInputMode="adjustResize"
android:exported="true"
>
</activity>
<activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
....
The issue has been solved, basically what I did earlier was that I Placed splash_screen.png in the drawable folder which worked perfectly for Android 7+ but was causing problems for Android 7 and lower.
So I placed the same splash_screen.png in all mipmaps folders and referred to mipmaps as follows in the launch_screen.xml file.
android:src="#mipmap/launch_screen"
After that splash screen worked perfectly for all the versions. Thanks

react-native-push-notification is undefined

Im trying to use react-native-push-notifications. Its all working on iOS but on android I get undefined is not an object(evaluating RNPushnotification.getInitialNotifications).
Here's my build.gradle
dependencies {
compile project(':react-native-push-notification')
compile project(':react-native-vector-icons')
compile fileTree(dir: "libs", include: ["*.jar"])
compile "com.android.support:appcompat-v7:23.0.1"
compile "com.facebook.react:react-native:+" // From node_modules
compile project(':react-native-push-notification')
compile ('com.google.android.gms:play-services-gcm:8.1.0') {
force = true;
}
}
This is my manifestfile:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tzawajna"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />
<permission
android:name="${applicationId}.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.tzawajna.permission.C2D_MESSAGE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="22" />
<application
android:name=".MainApplication"
android:allowBackup="true"
android:label="#string/app_name"
android:icon="#mipmap/ic_launcher"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
<receiver
android:name="com.google.android.gms.gcm.GcmReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.tzawajna" />
</intent-filter>
</receiver>
<receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationPublisher" />
<receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationBootEventReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationRegistrationService"/>
<service
android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationListenerService"
android:exported="false" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
</application>
</manifest>
this is my MainApplication.java
package (My app);
import android.app.Application;
import com.facebook.react.ReactApplication;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;
import com.facebook.react.shell.MainReactPackage;
import com.facebook.soloader.SoLoader;
import java.util.Arrays;
import java.util.List;
public class MainApplication extends Application implements ReactApplication {
private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
#Override
public boolean getUseDeveloperSupport() {
return BuildConfig.DEBUG;
}
#Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage()
);
}
};
#Override
public ReactNativeHost getReactNativeHost() {
return mReactNativeHost;
}
#Override
public void onCreate() {
super.onCreate();
SoLoader.init(this, /* native exopackage */ false);
}
}
I used react-native link and telling me that react-native-push-notifications has been linked. this is why i didnt added anything to the java file. I tried to add the asked code then the debuger tells me that theres an error with the javatojava
Please help.
I can see that the package is not linked to your java file.
add
import com.dieam.reactnativepushnotification.ReactNativePushNotificationPackage;
at the first of your MainApplication.java file also add new ReactNativePushNotificationPackage() to getPackages method return value so it looks like this :
#Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
new ReactNativePushNotificationPackage()
);
}
https://github.com/zo0r/react-native-push-notification/issues/728
worked for me.
compile project(':react-native-push-notification') was missing in build.gradle

Splash screen on Android

I have a problem with the splashscreen on Android. Here is everything what I am doing:
Creating background_splash.xml file:
xml version="1.0" encoding="utf-8"?>
layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="#android:color/darker_gray"/>
<item>
<bitmap
android:gravity="center"
android:src="#drawable/splash"/>
</item>
I remove < in the first two lines because stackoverflow cannot display them. This file is located in drawable.
I added the following to styles.xml:
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">#drawable/background_splash</item>
</style>
and this is my manifest files:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="22" />
<application
android:name=".MainApplication"
android:allowBackup="true"
android:label="#string/app_name"
android:icon="#mipmap/ic_launcher"
android:theme="#style/AppTheme">
<activity
android:name=".SplashActivity"
android:label="#string/app_name"
android:theme="#style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity" />
<activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
</application>
</manifest>
And also the activity:
import android.content.Intent;
import android.os.Bundle;
import com.facebook.react.ReactActivity;
public class SplashActivity extends ReactActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
}
}
This is the error I have:
Starting the app on 51d123cc (adb -s 51d123cc shell am start -n com.test/.MainActivity)...
Starting: Intent { cmp=com.test/.MainActivity }
java.lang.SecurityException: Permission Denial: starting Intent { flg=0x10000000 cmp=com.test/.MainActivity } from null (pid=20601, uid=2000) not exported from uid 11152
at android.os.Parcel.readException(Parcel.java:1540)
at android.os.Parcel.readException(Parcel.java:1493)
at android.app.ActivityManagerProxy.startActivityAsUser(ActivityManagerNative.java:2589)
at com.android.commands.am.Am.runStart(Am.java:768)
at com.android.commands.am.Am.onRun(Am.java:307)
at com.android.internal.os.BaseCommand.run(BaseCommand.java:47)
at com.android.commands.am.Am.main(Am.java:102)
at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:255)
UPDATE:
To the answer of #Ankit Prajapati I will add also that I set
android:largeHeap="true"
which was an error from Android Studio (running from the terminal this error wasn't reported) and this happen after changing activity to SplashActivity.
Also it was necessary to change edit configurations and changing "Launch Activity" to "Specified Activity" and setting com.test.SplashActivity.
Second way of managing splash screen on Android and which I use right now is with this tutorial:
Splash Screen Android
Try using this in
android:exported="true" in the manifest file in the activity you are trying to start (Loading Activity)
Like this
<activity
android:name=".SplashActivity"
android:label="#string/app_name"
android:theme="#style/SplashTheme"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Also Add
Intent intent = new Intent(SplashActivity.this, MainActivity.class);

Android: Branch.io tutorial: Branch.getAutoInstance(this);

I am trying to get Branch.io to work on Android, but I am running into:
myapplication.MainActivity cannot be cast to android.app.Application
I then changed:
Branch.getAutoInstance(this);
To:
Branch.getInstance();
In onCreate of the Activity.
Then I get:
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean io.branch.referral.Branch.initSession(io.branch.referral.Branch$BranchReferralInitListener, android.net.Uri, android.app.Activity)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
Can you help me get the basic up and running?
Following is my AndroidManifest.xml: (note: the branch_key is added in my app code)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.x.myapplication">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<meta-data android:name="io.branch.sdk.BranchKey" android:value="key_live_xxxxxxxxxxxxxxx" />
<activity android:name=".MainActivity">
<intent-filter>
<data android:scheme="yourapp" android:host="open" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="io.branch.referral.InstallListener" android:exported="true">
<intent-filter>
<action android:name="com.android.vending.INSTALL_REFERRER" />
</intent-filter>
</receiver>
</application>
</manifest>
My main Activity:
package com.example.chg.myapplication;
import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.util.Log;
import org.json.JSONObject;
import io.branch.referral.Branch;
import io.branch.referral.BranchError;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Branch.getAutoInstance(this);
Branch.getInstance();
setContentView(R.layout.activity_main);
}
#Override
public void onStart() {
super.onStart();
Branch branch = Branch.getInstance();
branch.initSession(new Branch.BranchReferralInitListener(){
#Override
public void onInitFinished(JSONObject referringParams, BranchError error) {
if (error == null) {
// params are the deep linked params associated with the link that the user clicked -> was re-directed to this app
// params will be empty if no data found
// ... insert custom logic here ...
} else {
Log.i("MyApp", error.getMessage());
}
}
}, this.getIntent().getData(), this);
}
#Override
public void onNewIntent(Intent intent) {
this.setIntent(intent);
}
}
Alex with Branch.io here:
We recently made some changes to our tutorial, and it looks like we missed a few things. I appreciate you posting about this — we'll be pushing an update later today for more clarity.
In this particular case, there are two issues:
A mixup between the Application onCreate() and Activity onCreate() methods, neither of which are actually needed for a basic implementation.
A missing Application class (we accidentally deleted this step from our tutorial completely — my apologies).
To get up and running, update your files as follows:
AndroidManifest.xml
You have three options here:
1. Use the Branch application class (easiest)
If you don't already have a custom application class, this is the simplest approach. Add android:name="io.branch.referral.BranchApp" to your Application class:
Edit: snippet UPDATED per comments below
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.chg.appbranch_01">
<meta-data android:name="io.branch.sdk.BranchKey" android:value="xxx" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme"
android:name="io.branch.referral.BranchApp">
<!--Enable test mode to see debugging data
(https://dev.branch.io/getting-started/integration-testing/guide/android/#use-debug-mode-to-simulate-fresh-installs)-->
<meta-data android:name="io.branch.sdk.TestMode" android:value="true" />
<meta-data android:name="io.branch.sdk.BranchKey" android:value="key_live_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<data android:scheme="theapp" android:host="open" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
</activity>
<receiver android:name="io.branch.referral.InstallListener" android:exported="true">
<intent-filter>
<action android:name="com.android.vending.INSTALL_REFERRER" />
</intent-filter>
</receiver>
</application>
</manifest>
2. Extend your Application class with the BranchApp class
If you already have a custom Application class, this is the simplest approach. Your AndroidManifext.xml file will look like this:
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme"
android:name="com.your.app.CustomApplicationClass" >
Your custom Application class (CustomApplicationClass in the example above) will look like this:
public final class CustomApplicationClass extends YourApp {
#Override
public void onCreate() {
super.onCreate();
}
}
3. Integrate directly into your custom application class
The most custom approach, for advanced implementations. You'd have your AndroidManifext.xml file set up the same as above:
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme"
android:name="com.your.app.CustomApplicationClass" >
And then configure your Application class as follows:
public final class CustomApplicationClass {
#Override
public void onCreate() {
super.onCreate();
Branch.getAutoInstance(this);
}
}
Activity Definition
Remove the onCreate() calls. They aren't needed here and are actually the cause of your error message (Branch.getAutoInstance(this) was passing the Activity context as this, when the SDK was expecting the Application context from option 3 above).
import io.branch.referral.Branch;
import io.branch.referral.BranchError;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public void onStart() {
super.onStart();
Branch branch = Branch.getInstance();
branch.initSession(new Branch.BranchReferralInitListener(){
#Override
public void onInitFinished(JSONObject referringParams, BranchError error) {
if (error == null) {
// params are the deep linked params associated with the link that the user clicked -> was re-directed to this app
// params will be empty if no data found
// ... insert custom logic here ...
} else {
Log.i("MyApp", error.getMessage());
}
}
}, this.getIntent().getData(), this);
}
#Override
public void onNewIntent(Intent intent) {
this.setIntent(intent);
}
}
Sorry for the inconvenience!

onConfigurationChange is never called

So i have simple code, i just want to change layout when i screen orientation is changed.
package com.example.asdasd;
import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onConfigurationChange(Configuration newConfig){
super.onConfigurationChanged(newConfig);
setContentView(R.layout.asdasd);
System.out.println("orientation---"+getResources().getConfiguration().orientation);
}
}
my manifest is
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.asdasd"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.asdasd.MainActivity"
android:label="#string/app_name"
android:configChanges="keyboardHidden|orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
So when i try this out on emulator and use ctrl+f12 or 9and 7 on num, screen flips but nothing happends and my layout stays the same, it wont work. Its like onConfigurationChange is never called and System.out.println is never reached.