通知不适用于 phone

Notifications do not work on phone

我在构建通知系统时遇到问题:
1. 每天在 20:00
发送通知 2. 正在创建特定于特定日期和时间的通知。

重复通知在虚拟设备上起作用,但不是每次都起作用。 第二个通知也只在某些时候有效,而且在我的真实 phone 上,没有任何工作正常。

我搜索了很多,但一直没有找到稳定广播通知的好的解决方案。

public class processingNotificationTask extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {

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

    Intent repeatingIntent = new Intent(context, Main.class);
    repeatingIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 1, repeatingIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder builder = (NotificationCompat.Builder) new NotificationCompat.Builder(context)
            .setContentTitle("test task")
            .setContentIntent(pendingIntent)
            .setSmallIcon(android.R.drawable.sym_def_app_icon)
            .setContentText("body")
            .setAutoCancel(true);
    notificationManager.notify(1, builder.build());
}



public class processingNotification extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {

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

    Intent repeatingIntent = new Intent(context, Main.class);
    repeatingIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 100, repeatingIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder builder = (NotificationCompat.Builder) new NotificationCompat.Builder(context)
            .setContentTitle("test")
            .setContentIntent(pendingIntent)
            .setSmallIcon(android.R.drawable.sym_def_app_icon)
            .setContentText("body")
            .setAutoCancel(true);
    notificationManager.notify(100, builder.build());
}
}

这里是重复通知的代码

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_hauptseite);
    context = this.getWindow().getContext();

    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 20);
    calendar.set(Calendar.MINUTE, 00);
    calendar.set(Calendar.SECOND, 0);
    Intent intent = new Intent(context, processingNotification.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager alarmManager =(AlarmManager) getSystemService(ALARM_SERVICE);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

这里是特定时间通知的代码(方法获取时间和日期并进行转换)

 public static void createNotification(String time, String day) {

    String parts[] = time.split("");
    String hour = part[1] + "" + part[2];
    String minute = part[4] + "" + part[5];



    int hourNumber = Integer.parseInt(hour);
    int minuteNumber = Integer.parseInt(minute);

    Calendar calendar = Calendar.getInstance();
    if (tag.equals("Monday")) {
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
    }
    if (tag.equals("Tuesday")) {
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
    }
    if (tag.equals("Wednesday")) {
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);
    }
    if (tag.equals("Thursday")) {
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);
    }
    if (tag.equals("Friday")) {
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
    }
    if (tag.equals("Saturday")) {
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
    }
    if (tag.equals("Sunday")) {
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
    }

    calendar.set(Calendar.HOUR_OF_DAY, hourNumber);
    calendar.set(Calendar.MINUTE, minuteNumber);
    calendar.set(Calendar.SECOND, 0);
    Intent intent = new Intent(context, processingNotificationTask.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager alarmManager =(AlarmManager) context.getSystemService(ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);



}

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.orcanianstudio.yourweek">

<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>

<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<uses-permission android:name="android.permission.SET_ALARM"/>


<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:screenOrientation="portrait"
    android:theme="@style/AppTheme">
    <activity android:name=".Main">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver android:name="Verarbeitung.verarbeitungNotification">
        <intent-filter>
            <action android:name="aufgabe.action.DISPLAY_NOTIFICATION"/>
        </intent-filter>

    </receiver>

    <receiver android:name="Verarbeitung.verarbeitungNotificationAufgabe">
        <intent-filter>
            <action android:name="aufgabe.action.DISPLAY_NOTIFICATION"/>
        </intent-filter>

    </receiver>

</application>

</manifest>

时间和日期是正确的,所以它们不应该是问题所在

从 Android 8.0(API 级别 26)开始,所有通知都必须分配给一个频道,否则它不会出现。我想这就是你的问题。

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

    String channelId = "1";
    CharSequence channelName = context.getString(R.string.channel_name_string);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel notificationChannel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH);
        notificationChannel.enableLights(true);
        if (notificationManager != null) {
            notificationManager.createNotificationChannel(notificationChannel);
        }
    }

这只是一小段代码,你应该google要完整的代码,应该不难找到。