Android 8.1.0来电通知未在启动器/主页上显示

时间:2018-04-27 12:42:53

标签: java android

我对android 8.1.0应用程序有疑问。当应用程序位于启动器页面或主页时,不会显示来电通知。如果应用程序在其他页面中,则来电将页面更改为电话页面。请帮忙。

1 个答案:

答案 0 :(得分:0)

如果你的应用程序在上面的targetsdkversion 27然后尝试在Android 8.0上面的设备中显示通知,你需要通道ID来显示通知。 我提供了简单的通知代码来显示android 8.0设备中的通知。

制作一种创建频道的方法,如下所示。

public void createChannels(String channel1) {
    // create android channel
    mChannleId =channel1;// show channel mChannleName.
    NotificationChannel androidChannel = new NotificationChannel(channel1,
            mChannleName, NotificationManager.IMPORTANCE_DEFAULT);
    //mChannel_Id=channel1;
    // Sets whether notifications posted to this channel should display notification lights
    androidChannel.enableLights(true);
    androidChannel.setDescription(mDescription);
    androidChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
    // Sets whether notification posted to this channel should vibrate.
    androidChannel.enableVibration(true);
    // Sets the notification light color for notifications posted to this channel
    androidChannel.setLightColor(Color.GREEN);
    // Sets whether notifications posted to this channel appear on the lockscreen or not
    androidChannel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
    androidChannel.setShowBadge(true);
    getManager().createNotificationChannel(androidChannel);
    Toast.makeText(getApplicationContext(),"Channel created",Toast.LENGTH_SHORT).show();
}

然后在创建通知后

 public Notification.Builder createNotification(String message)
{
    Intent resultIntent = new Intent(this, ResultActivity.class);
    resultIntent.putExtra("message",message);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addParentStack(ResultActivity.class);
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent =
            stackBuilder.getPendingIntent(
                    0,
                    PendingIntent.FLAG_UPDATE_CURRENT
            );

    return new Notification.Builder(getApplicationContext(),mChannel_Id)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(mChannel_Id)
            .setContentText(message)
            //.setGroup(group)
            .setChannelId(mChannel_Id)
            .setContentIntent(resultPendingIntent);
}

更多信息获取链接下面的参考.. - 有关使用channel_id和group创建各种类型通知的详细信息.-- https://developer.android.com/guide/topics/ui/notifiers/notifications.html

- https://code.tutsplus.com/tutorials/android-o-how-to-use-notification-channels - CMS-28616