API26及更高版本中的通知将不会显示,但在较低的API中将显示

时间:2017-11-06 05:06:05

标签: android push-notification android-emulator

我已经为API26编写了相同的通知,使用if-else编写了更高和更低的通知,我有一个Alarm_Receiver,在Manifest中,我也编写了(我的Receiver Android名称和Intent Filter以及我的服务android名称)。 API低于26的通知工作正常,但在API 26和API 27中它只在应用程序打开时关闭应用程序时工作,然后在闹钟时我收到错误“应用程序已停止”并通知不会是节目。 你能帮帮我,我该怎么办?

还有一件事 当我的模拟器以API 27启动时,我收到错误“系统没有响应”。关闭应用程序或等待。 但是,如果我等待,那么我的应用程序将运行,当应用程序打开时,我得到通知,但当我关闭应用程序,然后我没有得到通知,我得到一个错误“应用程序已停止”。 仿真器是5.1 WVGA 分辨率480x800和RAM 512。

我不是专业。我是Android Studio的新手。

   public int onStartCommand(Intent intent, int flags, int startId) {
    if(Build.VERSION.SDK_INT >= 26){

        NotificationManager mNotificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        String idsh = "my_channel_01";
        int notificationID = 1;
        CharSequence name = getString(R.string.hello_world);
        String description = getString(R.string.hello_world);
        int importance = NotificationManager.IMPORTANCE_HIGH;

        Uri sounduri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);


        NotificationChannel mChannel = new NotificationChannel(idsh, name, importance);
// Configure the notification channel.
        mChannel.setDescription(description);
        mChannel.enableLights(true);
        mChannel.setLightColor(Color.RED);
        mChannel.enableVibration(true);
        mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
        mChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
        assert mNotificationManager != null;
        mNotificationManager.createNotificationChannel(mChannel);
        Intent resultIntent = new Intent(this, MainActivity.class);
        PendingIntent resultPendingIntent =
                PendingIntent.getActivity(
                        this,
                        0,
                        resultIntent,
                        PendingIntent.FLAG_UPDATE_CURRENT
                );



        Notification notification = new Notification.Builder(this,idsh)
                .setContentTitle("Hello World")
                .setContentText("Hellooooo")
                .setBadgeIconType(R.mipmap.ic_launcher)
                .setNumber(5)
                .setContentIntent(resultPendingIntent)
                .setSmallIcon(R.mipmap.ic_launcher_round)
                .setAutoCancel(true)
                .build();



        assert mNotificationManager != null;
        mNotificationManager.notify(notificationID, notification);

        Log.i("LocalService", "Received start id " + startId + ": " + intent);

        }else{
        Uri sounduri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.ic_launcher_background)
                        .setSound(sounduri)
                        .setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 })
                        .setContentTitle("Hello World")
                        .setContentText("Hello World");
        Intent resultIntent = new Intent(this, MainActivity.class);
        // Because clicking the notification opens a new ("special") activity, there's

        PendingIntent resultPendingIntent =
                PendingIntent.getActivity(
                        this,
                        0,
                        resultIntent,
                        PendingIntent.FLAG_UPDATE_CURRENT
                );

        mBuilder.setContentIntent(resultPendingIntent);
        // Sets an ID for the notification
        int mNotificationId = 001;

        NotificationManager mNotifyMgr =
                (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        assert mNotifyMgr != null;
        mNotifyMgr.notify(mNotificationId, mBuilder.build());

        }

首先我在Logcat中遇到此错误:

android.gms.persistent E/GcmPushIntentOperation: Failed to subscribe to GCM: com.google.android.inputmethod.latin
                                                                                     java.io.IOException: TIMEOUT
                                                                                         at vab.b(:com.google.android.gms@11577470:42)
                                                                                         at vab.a(:com.google.android.gms@11577470:46)
                                                                                         at uzy.b(:com.google.android.gms@11577470:11)
                                                                                         at uzy.a(:com.google.android.gms@11577470:29)
                                                                                         at com.google.android.gms.phenotype.gcmpush.GcmPushIntentOperation.a(:com.google.android.gms@11577470:22)
                                                                                         at com.google.android.gms.phenotype.gcmpush.GcmPushIntentOperation.onHandleIntent(:com.google.android.gms@11577470:6)
                                                                                         at com.google.android.chimera.IntentOperation.onHandleIntent(:com.google.android.gms@11577470:2)
                                                                                         at bwh.run(:com.google.android.gms@11577470:10)
                                                                                         at bwe.run(:com.google.android.gms@11577470:14)
                                                                                         at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
                                                                                         at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
                                                                                         at java.lang.Thread.run(Thread.java:764)

和此:

E / ConfigUpdater:忽略更新请求

然后在闹钟时间后一分钟我进入模拟器错误:应用已停止并且在Logcat中:

 Process: com.example.standard.alarm, PID: 3903
                                                                      java.lang.RuntimeException: Unable to start receiver com.example.standard.alarm.Alarm_Receiver: java.lang.IllegalStateException: Not allowed to start service Intent { act=com.demo.FirebaseMessagingReceiveService cmp=com.example.standard.alarm/.RingtonePlayingService }: app is in background uid UidRecord{8d38734 u0a79 RCVR idle change:uncached procs:1 seq(0,0,0)}
                                                                          at android.app.ActivityThread.handleReceiver(ActivityThread.java:3194)
                                                                          at android.app.ActivityThread.-wrap17(Unknown Source:0)
                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1672)
                                                                          at android.os.Handler.dispatchMessage(Handler.java:106)
                                                                          at android.os.Looper.loop(Looper.java:164)
                                                                          at android.app.ActivityThread.main(ActivityThread.java:6494)
                                                                          at java.lang.reflect.Method.invoke(Native Method)
                                                                          at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
                                                                       Caused by: java.lang.IllegalStateException: Not allowed to start service Intent { act=com.demo.FirebaseMessagingReceiveService cmp=com.example.standard.alarm/.RingtonePlayingService }: app is in background uid UidRecord{8d38734 u0a79 RCVR idle change:uncached procs:1 seq(0,0,0)}
                                                                          at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1518)
                                                                          at android.app.ContextImpl.startService(ContextImpl.java:1474)
                                                                          at android.content.ContextWrapper.startService(ContextWrapper.java:649)
                                                                          at android.content.ContextWrapper.startService(ContextWrapper.java:649)
                                                                          at com.example.standard.alarm.Alarm_Receiver.onReceive(Alarm_Receiver.java:15)
                                                                          at android.app.ActivityThread.handleReceiver(ActivityThread.java:3187)

1 个答案:

答案 0 :(得分:0)

你得到Not allowed to start service Intent when app is in background异常原因来自API级别26(Oreo)android已对后台服务执行施加限制。你可以在这里阅读相同的Background executions limit

您可以使用startForegroundService() startforeground在前台启动服务,但在获得ANR之前必须先启动服务。

其他选项是使用JobScheduler JobScheduler并使用JobScheduler替换您的服务以执行所需的任务。