AlarmManager和通知 - 通知不显示,报警不应该触发

时间:2018-05-15 14:05:56

标签: android android-notifications

我正在为客户开发我的第一个Android应用程序。发布会定于周六举行,一切正常......但有一点。

我需要应用程序连接到客户的网站每3天,下载活动信息,然后显示通知并(在某些情况下)发送电子邮件。

现在正在发生的事情是应用程序没有显示任何通知,而是每天发送多封邮件:这应该意味着警报每天多次触发,而不是每3次触发一次。

在我的清单中,我有这两个接收器:

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

[...]

<receiver android:name=".Helper.AlarmDownload"/>
    <receiver
        android:name=".Helper.AlarmBootReceiver"
        android:enabled="false">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
        </intent-filter>
    </receiver>

AlarmDownload.java 做了很多事情。它们都有效,所以我不会用完整的代码来惹恼你。结束时:

Intent intentToRepeat = new Intent(context, Caricamento.class);
                        intentToRepeat.putExtra(Costanti.string_notif_idcorso,notifica.getInt(Costanti.sp_notifiche_idcorso,0));
                        //set flag to restart/relaunch the app
                        intentToRepeat.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

                        //Pending intent to handle launch of Activity in intent above
                        PendingIntent pendingIntent =
                                PendingIntent.getActivity(context, NotificationHelper.ALARM_TYPE_RTC, intentToRepeat, PendingIntent.FLAG_UPDATE_CURRENT);

                        //Build notification
                        String titolonotifica = notifica.getString(Costanti.sp_notifiche_titolonotifica,"");
                        String subtitolo = notifica.getString(Costanti.sp_notifiche_subtitolo,"");
                        Notification repeatedNotification = buildLocalNotification(context, pendingIntent,titolonotifica,subtitolo).build();


                        //Send local notification
                        NotificationHelper.getNotificationManager(context).notify(NotificationHelper.ALARM_TYPE_RTC, repeatedNotification);

[...]

public NotificationCompat.Builder buildLocalNotification(Context context, PendingIntent pendingIntent, String titolo, String subtitolo) {
    NotificationCompat.Builder mBuilder;

    mBuilder = new NotificationCompat.Builder(context, "fisicamente")
            .setContentIntent(pendingIntent)
            .setSmallIcon(R.drawable.icona_app)
            .setContentTitle(titolo)
            .setContentText(subtitolo)
            //.setStyle(new NotificationCompat.BigTextStyle()
            //.bigText(testo))
            .setAutoCancel(true)
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .setCategory(NotificationCompat.CATEGORY_EVENT);

    return mBuilder;
}

所以,真正的问题应该是 NotificationHelper.java

public class NotificationHelper {
public static int ALARM_TYPE_RTC = 100;
private static AlarmManager alarmManager1;
private static PendingIntent alarmIntent1;

public static void scheduleRepeatingRTCNotification(Context context) {
    SharedPreferences pref_login = context.getSharedPreferences(Costanti.SP_LOGIN, Context.MODE_PRIVATE);
    Boolean primavolta = pref_login.getBoolean("primavolta",false);
//primavolta means first time
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    Random r1 = new Random();
    int casuale1 = r1.nextInt(100);
    calendar.set(Calendar.HOUR_OF_DAY, 11);
    calendar.set(Calendar.MINUTE, casuale1);
    if (primavolta && calendar.before(Calendar.getInstance())) calendar.add(Calendar.DAY_OF_MONTH, 1);
//I don't want to show the notification on the first login of the user, so i'm adding one day to the calendar.

    Intent intent = new Intent(context, AlarmDownload.class);
    alarmIntent1 = PendingIntent.getBroadcast(context, ALARM_TYPE_RTC, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    alarmManager1 = (AlarmManager)context.getSystemService(ALARM_SERVICE);

        alarmManager1.setInexactRepeating(AlarmManager.RTC,
                calendar.getTimeInMillis(), 3 * AlarmManager.INTERVAL_DAY, alarmIntent1);

}

这是我在 MainActivity.java 中所做的,这是用户第一次登录时:

Boolean primavolta = preferenze.getBoolean("primavolta",false);
    if (primavolta) {
        //Apre il drawer
        drawer.openDrawer(GravityCompat.START);
        //Notifiche
NotificationHelper.scheduleRepeatingRTCNotification(getApplicationContext());

    }

抽屉仅在首次登录时打开,因此schedulerepeatingRTCNotification void不会多次触发。

有什么想法吗?

非常感谢你!

编辑:代码中有一个costant而不是3的引用。

0 个答案:

没有答案