通知未出现在 notify() 上

Notification Not Appearing on notify()

我最近在 Android Studio 上使用许多特定功能时遇到了困难。最近我正在尝试显示简单的通知。他们从不露面。我觉得我在这一点上已经尝试了一切。这是我的代码。

public class NotificationReceiver extends BroadcastReceiver {

private final String TAG = NotificationReceiver.class.getSimpleName();

@Override
public void onReceive(Context context, Intent intent) {
    Intent notificationIntent = new Intent(context, ScrollingActivity.class);

    TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(context);
    taskStackBuilder.addParentStack(ScrollingActivity.class);
    taskStackBuilder.addNextIntent(notificationIntent);

    PendingIntent pendingIntent = taskStackBuilder.getPendingIntent(100, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "channel");

    Notification notification = builder.setContentTitle("My Test Notification")
            .setContentText("This is some sample test notification. (Congratulations!)")
            .setTicker("New Message Alert!")
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentIntent(pendingIntent).build();

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0, notification);
}}

这是我用来调用通知的方法。

 public void setRepeatingNotification(){
    Calendar repeatCalendar = Calendar.getInstance();

    repeatCalendar.set(Calendar.HOUR_OF_DAY, 21);
    repeatCalendar.set(Calendar.MINUTE, 40);
    repeatCalendar.set(Calendar.SECOND, 10);

    Intent intent = new Intent(ScrollingActivity.this, NotificationReceiver.class);

    PendingIntent pendingIntent = PendingIntent.getBroadcast(ScrollingActivity.this, 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, repeatCalendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

    Toast.makeText(ScrollingActivity.this, "Repeating Notification Set", Toast.LENGTH_LONG).show();
}

这是清单。

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.SET_ALARM"/>
<uses-permission android:name="android.permission.INTERNET"/>
<application
    android:name=".MainApplication"
    android:allowBackup="true"
    android:icon="@mipmap/skillset_v3"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
    >
    <activity
        android:name=".ScrollingActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".TaskViewActivity"
        android:label="@string/title_activity_task_view"
        android:parentActivityName=".ScrollingActivity"
        android:screenOrientation="portrait"
        android:theme="@style/AppTheme.NoActionBar">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.jermaineoliver.aswitch.ScrollingActivity" />
    </activity>
    <activity
        android:name=".MasteryViewActivity"
        android:label="@string/title_activity_mastery_view"
        android:parentActivityName=".ScrollingActivity"
        android:screenOrientation="portrait"
        android:theme="@style/AppTheme.NoActionBar">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.jermaineoliver.aswitch.ScrollingActivity" />
    </activity>
    <service
        android:name="TaskTrackingService"/>
    <receiver android:name=".NotificationReceiver"/>
</application>

请帮忙!我一直在研究这个。

惊讶的是没有人跳上这个。我找到了答案。 API 级别不匹配。显然,使用高于 Oreo 的版本的通知需要 NotificationChannel。当我在较低的 API 上尝试时,它起作用了。知道那种让我的手能够让它发挥作用。如果其他人有这个问题让我知道,也许我可以提供帮助。

与NotificationChannel有关。在 Android Oreo 及更高版本中,您可以像这样创建一个 NotificationChannel:

NotificationManager notificationManager =
    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

String channelId = "my_channel_id";
CharSequence channelName = "My Channel";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel notificationChannel = new NotificationChannel(channelId,             channelName, importance);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(true);
notificationChannel.setVibrationPattern(new long[]{1000, 2000});
notificationManager.createNotificationChannel(notificationChannel);

然后你创建你的通知如下:

NotificationManager notificationManager = 
    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int notifyId = 1;
String channelId = "my_channel_id";

Notification notification = new Notification.Builder(MainActivity.this)
    .setContentTitle("My Message")
    .setContentText("My test message!")
    .setSmallIcon(R.drawable.ic_notification)
    .setChannel(channelId)
    .build();

notificationManager.notify(id, notification);

这样通知将使用正确的通知渠道并正确显示。您还可以为通知渠道创建组。在这里阅读更多:

Documenation - NotificationChannel

Examples