首页 > 解决方案 > 本地通知在 Android 8.0 Oreo 中不起作用

问题描述

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

MainActivity.Here 我正在调用 Service。

 Intent sync = new Intent(ctx,NotificationService.class);
 startService(sync);

NotificationService.class.OnCreate 方法

    notificationManager = new SipNotifications(this);
        notificationManager.onServiceCreate();

 Intent notificationIntent = new Intent("android.media.action.DISPLAY_NOTIFICATION");
                       notificationIntent.addCategory("android.intent.category.DEFAULT");

                       PendingIntent broadcast = PendingIntent.getBroadcast(this, 100, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

                       AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

                       Calendar cal = Calendar.getInstance();
                       //cal.add(Calendar.HOUR, 24);
                       cal.add(Calendar.HOUR_OF_DAY,24);
                       am.setExact(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), broadcast);

Alarm_Receiver 类 onReceive 方法

Intent notificationIntent = new Intent(context, MainActivity.class);

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    stackBuilder.addParentStack(MainActivity.class);
    stackBuilder.addNextIntent(notificationIntent);

    PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
    System.out.println("Alarm Receiver Notification"+"In Notification");
    notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        /* Create or update. */
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID, context.getResources().getString(R.string.app_name),
                NotificationManager.IMPORTANCE_DEFAULT);

        notificationManager.createNotificationChannel(channel);
    }


    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
    Notification notification;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
         notification = builder.setContentTitle("coins.")
                .setContentText("Your coins are ready to collect.")
                .setTicker("New Message Alert!")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setAutoCancel(true)
                 .setChannelId(CHANNEL_ID)
                .setContentIntent(pendingIntent).build();
    }else {
         notification = builder.setContentTitle("coins.")
                .setContentText("Your coins are ready to collect.")
                .setTicker("New Message Alert!")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setAutoCancel(true)
                .setContentIntent(pendingIntent).build();
    }
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0, notification);

标签: androidserviceandroid-broadcastreceiver

解决方案


尝试这个

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");
        }
    });

推荐阅读