使用alt信标可以在oreo上正常工作,但是在pie上崩溃

时间:2019-01-09 09:01:10

标签: android altbeacon

我已经使用Alt beacon开发了一个信标应用程序。它在oreo上可以正常工作,但是由于通知问题而在android pie上崩溃了。请帮帮我。 一切正常,但应用程序无法正常运行。我在安装时崩溃。我在One plus 6t mobile上尝试过。 2)我也无法在模拟器中运行。如何在模拟器中测试此应用程序。 崩溃报告是:

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)

应用程序代码为:

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();

    }

}
}

1 个答案:

答案 0 :(得分:0)

由于某种原因,Android在此行中未成功创建您的NotificationChannel:

notificationManager.createNotificationChannel(channel);

您可以看到由于未找到通知渠道而here引发的异常的Android源代码。

您可以先尝试验证其通知渠道是否使用以下代码创建:

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

然后验证channelActuallyCreated不为null。在开始创建频道之前,您可能会使用不同的参数值。