通知无法在手机

时间:2018-03-16 14:09:55

标签: android notifications broadcastreceiver

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

重复通知正在运行虚拟设备但不是每次都有效。 第二个通知也只在一些时间内工作,在我的真实手机上,没有任何工作正常。

我搜索了很多,但我从未找到稳定广播通知的好解决方案。

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>

时间和日期都是正确的,所以他们不应该成为问题

1 个答案:

答案 0 :(得分:1)

从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完整代码,不应该很难找到。