Using alt beacon is correctly working on oreo, but crashes on pie

0

I have developed a beacon app using Alt beacon.It was working correctly on oreo but crashes on android pie because of notification problem.Kindly help me out. Everything working fine,but app is not openeing in pie.It crashes while i am installing.i tried on One plus 6t mobile. 2)Also i am not able to run it in emulator.how to test this app in emulator. The crash report is :

Fatal Exception: android.app.RemoteServiceException: Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification: Notification(channel=My Notification Channel ID pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x40 color=0x00000000 vis=PRIVATE)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1835)
   at android.os.Handler.dispatchMessage(Handler.java:106)
   at android.os.Looper.loop(Looper.java:193)
   at android.app.ActivityThread.main(ActivityThread.java:6863)
   at java.lang.reflect.Method.invoke(Method.java)
   at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

The application code is:

public class MyApplication extends Application implements BootstrapNotifier {
private static final String TAG = ".RangingActivity";
private RegionBootstrap regionBootstrap;
private Identifier identifier;
private BeaconManager beaconManager;
private Region region;
public static final String CHANNEL_ID="Geotoppng";
private NavigationMainContract.getshareddata getshareddata;
private NavigationAppRepository navigationAppRepository;


@Override
public void onCreate() {

    Log.d(TAG, "App started up");


    getshareddata=new Sharedpreferncedatasource(this);
    navigationAppRepository=new NavigationAppRepository(getshareddata);

    beaconManager = BeaconManager.getInstanceForApplication(this);

    beaconManager.getBeaconParsers().add(new BeaconParser()
            .setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25"));

    identifier = Identifier.parse("E2C56DB5-KEKL-48D2-B060-D0F5A71096E0"); //beacon 1
    beaconManager.setBackgroundBetweenScanPeriod(3001);
    beaconManager.setBackgroundScanPeriod(5001);



    Notification.Builder builder = new Notification.Builder(this);
    builder.setSmallIcon(R.mipmap.ic_launcher);
    builder.setContentTitle("Scanning for Beacons");
    Intent intent = new Intent(this, SplashActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(
            this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT
    );
    builder.setContentIntent(pendingIntent);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel("My Notification ID",
                "My Notification Name", NotificationManager.IMPORTANCE_DEFAULT);
        channel.setDescription("My Notification Channel Description");
        NotificationManager notificationManager = (NotificationManager) getSystemService(
                Context.NOTIFICATION_SERVICE);
        notificationManager.createNotificationChannel(channel);
        builder.setChannelId(channel.getId());
    }
    beaconManager.enableForegroundServiceScanning(builder.build(), 456);
    beaconManager.setEnableScheduledScanJobs(false);

     region = new Region("com.example.myapp.boostrapRegion", identifier, null, null);
    regionBootstrap = new RegionBootstrap(this, region);

    BeaconManager.setBeaconSimulator(new TimedBeaconSimulator() );
    ((TimedBeaconSimulator) BeaconManager.getBeaconSimulator()).createTimedSimulatedBeacons();
    super.onCreate();
}

@Override
public void didEnterRegion(Region region) {
    regionBootstrap.disable();

    Log.i(TAG, "Entered region");
    Toast.makeText(this, "Entered region..wait", Toast.LENGTH_SHORT).show();
    Intent intent = new Intent(this, NavigationActivity.class);
    // IMPORTANT: in the AndroidManifest.xml definition of this activity, you must set android:launchMode="singleInstance" or you will get two instances
    // created when a user launches the activity manually and it gets launched from here.
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    this.startActivity(intent);
    /*if(navigationAppRepository.isScannerFagmentAlreadyLaunched()){
        Log.i(TAG, "Application_didenterregion_Alreadylaunched"+navigationAppRepository.isScannerFagmentAlreadyLaunched());


    }
    else {
        Log.i(TAG, "Application_didenterregion_Not launched");
        Toast.makeText(this, "entered", Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(this, NavigationActivity.class);
        // IMPORTANT: in the AndroidManifest.xml definition of this activity, you must set android:launchMode="singleInstance" or you will get two instances
        // created when a user launches the activity manually and it gets launched from here.
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        this.startActivity(intent);


    }*/

}

@Override
public void didExitRegion(Region region) {
    Log.i(TAG, "Exit region");
}

@Override
public void didDetermineStateForRegion(int i, Region region) {

    if(i==0){
        Constantsforbinding.application_stateregion=false;
        Log.i(TAG, "Application_offed"+Constantsforbinding.application_stateregion);
        Toast.makeText(this, "No beacon", Toast.LENGTH_SHORT).show();
    }
    else {
        Constantsforbinding.application_stateregion=true;
        Log.i(TAG, "Application_onned"+Constantsforbinding.application_stateregion);
        Toast.makeText(this, "Fetching...", Toast.LENGTH_SHORT).show();

    }

}
}
android
altbeacon
asked on Stack Overflow Jan 9, 2019 by kavinmkk6 • edited Jan 9, 2019 by Phantômaxx

1 Answer

0

For some reason, Android is not creating your NotificationChannel successfully in this line:

notificationManager.createNotificationChannel(channel);

You can see the Android source code that throws the exception you see as a result of the notification channel not being found here.

You might try first verifying it your notification channel was created with code like this:

NotificationChannel channelActuallyCreated = 
  notificationManager.getNotificationChannel(channel.getId());

And then verify that channelActuallyCreated is not null. You might play with different parameter values in your channel creation until this starts working.

answered on Stack Overflow Jan 9, 2019 by davidgyoung

User contributions licensed under CC BY-SA 3.0