Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/reporting-services/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
本地通知在Android 8.0 Oreo中不起作用_Android_Service_Android Broadcastreceiver - Fatal编程技术网

本地通知在Android 8.0 Oreo中不起作用

本地通知在Android 8.0 Oreo中不起作用,android,service,android-broadcastreceiver,Android,Service,Android Broadcastreceiver,我在我的应用程序中使用了本地初始化。用户每天都会收到通知。在低于8.0的版本中工作正常,但在8.0版本中没有。这是我的代码 主要活动。我打电话给服务部 NotificationService.class.OnCreate方法 报警接收器类 接收法 你可以用这个方法。现在有了最新的API版本,您需要为通知设置通道 private static final String NOTIFICATION_CHANNEL_ID ="notification_channel_id"; private stati

我在我的应用程序中使用了本地初始化。用户每天都会收到通知。在低于8.0的版本中工作正常,但在8.0版本中没有。这是我的代码

主要活动。我打电话给服务部

NotificationService.class.OnCreate方法

报警接收器类 接收法


你可以用这个方法。现在有了最新的API版本,您需要为通知设置通道

private static final String NOTIFICATION_CHANNEL_ID ="notification_channel_id";
private static final String NOTIFICATION_Service_CHANNEL_ID = "service_channel";
 .....
private void startInForeground() {
int icon = R.mipmap.icon;

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
    icon = R.mipmap.icon_transparent;
}

Intent notificationIntent = new Intent(this, CurrentActivity.class);
PendingIntent pendingIntent=PendingIntent.getActivity(this,0,notificationIntent,0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
    .setSmallIcon(icon)
    .setContentIntent(pendingIntent)
    .setContentTitle("Service")
    .setContentText("Running...");
Notification notification=builder.build();
if(Build.VERSION.SDK_INT>=26) {
    NotificationChannel channel = new NotificationChannel(NOTIFICATION_Service_CHANNEL_ID, "Sync Service", NotificationManager.IMPORTANCE_HIGH);
    channel.setDescription("Service Name");
    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.createNotificationChannel(channel);

    notification = new Notification.Builder(this,NOTIFICATION_Service_CHANNEL_ID)
        .setContentTitle("Service")
        .setContentText("Running...")
        .setSmallIcon(icon)
        .setContentIntent(pendingIntent)
        .build();
}
startForeground(121, notification);
}

你可以用这个方法。现在有了最新的API版本,您需要为通知设置通道

private static final String NOTIFICATION_CHANNEL_ID ="notification_channel_id";
private static final String NOTIFICATION_Service_CHANNEL_ID = "service_channel";
 .....
private void startInForeground() {
int icon = R.mipmap.icon;

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
    icon = R.mipmap.icon_transparent;
}

Intent notificationIntent = new Intent(this, CurrentActivity.class);
PendingIntent pendingIntent=PendingIntent.getActivity(this,0,notificationIntent,0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
    .setSmallIcon(icon)
    .setContentIntent(pendingIntent)
    .setContentTitle("Service")
    .setContentText("Running...");
Notification notification=builder.build();
if(Build.VERSION.SDK_INT>=26) {
    NotificationChannel channel = new NotificationChannel(NOTIFICATION_Service_CHANNEL_ID, "Sync Service", NotificationManager.IMPORTANCE_HIGH);
    channel.setDescription("Service Name");
    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.createNotificationChannel(channel);

    notification = new Notification.Builder(this,NOTIFICATION_Service_CHANNEL_ID)
        .setContentTitle("Service")
        .setContentText("Running...")
        .setSmallIcon(icon)
        .setContentIntent(pendingIntent)
        .build();
}
startForeground(121, notification);
}
试试这个

public class NotificationHelper {

private Context mContext;
private NotificationManager mNotificationManager;
private NotificationCompat.Builder mBuilder;
public static final String NOTIFICATION_CHANNEL_ID = "10001";

public NotificationHelper(Context context) {
    mContext = context;
}

/**
 * Create and push the notification 
 */
public void createNotification(String title, String message)
{    
    /**Creates an explicit intent for an Activity in your app**/
    Intent resultIntent = new Intent(mContext , SomeOtherActivity.class);
    resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    PendingIntent resultPendingIntent = PendingIntent.getActivity(mContext,
            0 /* Request code */, resultIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    mBuilder = new NotificationCompat.Builder(mContext);
    mBuilder.setSmallIcon(R.mipmap.ic_launcher);
    mBuilder.setContentTitle(title)
            .setContentText(message)
            .setAutoCancel(false)
            .setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
            .setContentIntent(resultPendingIntent);

    mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O)
    {
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", importance);
        notificationChannel.enableLights(true);
        notificationChannel.setLightColor(Color.RED);
        notificationChannel.enableVibration(true);
        notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
        assert mNotificationManager != null;
        mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
        mNotificationManager.createNotificationChannel(notificationChannel);
    }
    assert mNotificationManager != null;
    mNotificationManager.notify(0 /* Request Code */, mBuilder.build());
}
}
在您创建通知的类上

 notificationHelper=new NotificationHelper(this);
    Button btn=(Button)findViewById(R.id.btn);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
           notificationHelper. createNotification("Title","Message");
        }
    });
试试这个

public class NotificationHelper {

private Context mContext;
private NotificationManager mNotificationManager;
private NotificationCompat.Builder mBuilder;
public static final String NOTIFICATION_CHANNEL_ID = "10001";

public NotificationHelper(Context context) {
    mContext = context;
}

/**
 * Create and push the notification 
 */
public void createNotification(String title, String message)
{    
    /**Creates an explicit intent for an Activity in your app**/
    Intent resultIntent = new Intent(mContext , SomeOtherActivity.class);
    resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    PendingIntent resultPendingIntent = PendingIntent.getActivity(mContext,
            0 /* Request code */, resultIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    mBuilder = new NotificationCompat.Builder(mContext);
    mBuilder.setSmallIcon(R.mipmap.ic_launcher);
    mBuilder.setContentTitle(title)
            .setContentText(message)
            .setAutoCancel(false)
            .setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
            .setContentIntent(resultPendingIntent);

    mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O)
    {
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", importance);
        notificationChannel.enableLights(true);
        notificationChannel.setLightColor(Color.RED);
        notificationChannel.enableVibration(true);
        notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
        assert mNotificationManager != null;
        mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
        mNotificationManager.createNotificationChannel(notificationChannel);
    }
    assert mNotificationManager != null;
    mNotificationManager.notify(0 /* Request Code */, mBuilder.build());
}
}
在您创建通知的类上

 notificationHelper=new NotificationHelper(this);
    Button btn=(Button)findViewById(R.id.btn);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
           notificationHelper. createNotification("Title","Message");
        }
    });

我有alresdy use CHANNEL_ID。有没有其他方法解决这个问题?@Dharmesh patel我正在使用这个代码及其在Android 8.0 Oreo上的工作。如果这个答案是帮助完整的,那么就投票回答,这样其他用户也知道这个被接受的答案和工作代码。我有alresdy use CHANNEL_ID。有没有其他方法解决这个问题?@Dharmeshpatel我正在使用此代码及其在Android 8.0 Oreo上的工作。如果此答案是帮助完整,则投票答案,以便其他用户也知道此已接受的答案和工作代码。我有alresdy use CHANNEL_ID。是否有其他方法解决此问题?我有alresdy use CHANNEL_ID。是否有其他方法解决此问题?