ANDROID O通知不起作用.

0 mobile android

运用

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.TaskStackBuilder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.view.View;
Run Code Online (Sandbox Code Playgroud)


在1st Activity创建按钮上创建了名为secoundactivity的第二个活动,并调用以下方法

        String CHANNEL_ID = "my_channel_01";
    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.network_service)
                    .setContentTitle("My notification")
                    .setContentText("Hello World!")
            .setChannel(CHANNEL_ID);

                    //.setchannelid(CHANNEL_ID);

    Intent resultIntent = new Intent(this, secondactivity.class);

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addParentStack(secondactivity.class);
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent =
            stackBuilder.getPendingIntent(
                    0,
                    PendingIntent.FLAG_UPDATE_CURRENT
            );
    mBuilder.setContentIntent(resultPendingIntent);
    NotificationManager mNotificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    mNotificationManager.notify(0, mBuilder.build());
Run Code Online (Sandbox Code Playgroud)

并使用
Compile SDK版本:API 26:Android 8.0(O)
构建工具:26.0.1

请帮助,因为我只是新的

Adv*_*Dog 5

您正在通知中设置频道,但您需要先创建频道NotificationManager.

所以,首先要创建这样的频道.

val manager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
// Creating a channel - required for O's notifications.
val channel = NotificationChannel("my_channel_01",
         "Channel human readable title",
         NotificationManager.IMPORTANCE_DEFAULT)
manager.createNotificationChannel(channel)
Run Code Online (Sandbox Code Playgroud)

然后,您可以在构建通知时使用频道的ID.

// Building the notification.
val builder = Notification.Builder(context, channel.id)
// Using the channel's id in the builder.
builder.setChannelId(channel.id)
Run Code Online (Sandbox Code Playgroud)

最后,您可以发布您的通知.

// Posting the notification.
manager.notify(notificationId, builder.build())
Run Code Online (Sandbox Code Playgroud)