android - Android 中的每次 session 都不会触发通知

标签 android notifications alarmmanager

我正在开发一个客户 session 应用程序,用户可以在其中存储即将举行的 session 信息。有关 session 的通知必须在 session 时间前 2 小时发出。但实际情况是,通知仅针对添加的最后一个 session 触发。 这可能是因为我正在创建一个 PendingIntent PendingIntent.FLAG_UPDATE_CURRENT。 此外,使用 PendingIntent.FLAG_CANCEL_CURRENT 也无济于事。 添加下面评论中提到的请求代码无济于事 请提供一个解决方案,以便为每次 session 触发通知。

感谢您的帮助。

AlarmManager 代码:

private void setAlarm(int yyyy,int MM,int dd,int hh, String id){

        Calendar cal = Calendar.getInstance();
        cal.set(yyyy, MM, dd, (hh-2), 0);

        PendingIntent pending = PendingIntent.getBroadcast(getActivity(), 0,new Intent(getActivity(), notify.class).putExtra("id", id), PendingIntent.FLAG_UPDATE_CURRENT);

        AlarmManager alarm = (AlarmManager)getActivity().getSystemService(getActivity().ALARM_SERVICE);
        //alarm.cancel(pending);    //read what happens with cancel
        alarm.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pending);
    }

通知的代码:

public class notify extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
            String id = intent.getStringExtra("id");
            Datastore ds = new Datastore(context);
            SQLiteDatabase sql = ds.getReadableDatabase();
            Log.e("id in notify", id);
            Cursor curse= sql.rawQuery("SELECT NAME , CONTACT , DATE , TIME , ADDRESS , DETAILS FROM "+fields.table_name+" WHERE ID ='"+id+"' ", null);
            curse.moveToFirst();
            Bundle bundle = new Bundle();
            bundle.putString("id",id);
            bundle.putString("name", curse.getString(curse.getColumnIndex("NAME")));
            bundle.putString("contact", curse.getString(curse.getColumnIndex("CONTACT")));
            bundle.putString("date",curse.getString(curse.getColumnIndex("DATE")));
            bundle.putString("time", curse.getString(curse.getColumnIndex("TIME")));
            bundle.putString("address", curse.getString(curse.getColumnIndex("ADDRESS")));
            bundle.putString("details",curse.getString(curse.getColumnIndex("DETAILS")));


            PendingIntent pending = PendingIntent.getActivity(context, 0, new Intent(context, finaldetails.class).putExtras(bundle),PendingIntent.FLAG_UPDATE_CURRENT);
            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);

            Notification notification = mBuilder.setSmallIcon(R.drawable.ic_launcher).setTicker("You have a meeting").setWhen(0)
                    .setAutoCancel(true)
                    .setContentTitle("Upcoming Meeting")
                    .setContentText("You have a meeting with "+bundle.getString("name"))
                    .setStyle(new NotificationCompat.BigTextStyle().bigText("You have a meeting"))
                    .setContentIntent(pending)
                    .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                    .build();

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

    }

}

最佳答案

在以下代码中,您将指定的所有未决 Intent 的请求 ID 指定为零。要创建单独的警报,您需要为所有未决 Intent 使用不同的请求代码。既然一样,就只考虑最后一次报警。

PendingIntent pending = PendingIntent.getBroadcast(getActivity(), 0,new Intent(getActivity(), notify.class).putExtra("id", id), PendingIntent.FLAG_UPDATE_CURRENT);

关于android - Android 中的每次 session 都不会触发通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32478875/

相关文章:

java - 为 Android 应用程序编写汇编语言代码

java - Android:使用现成的 Django 数据库?

android - 在 ICS 的 CAN 接口(interface)中设置比特率

ios - UILocalNotification 不会在 ios9 设备上触发(横幅未显示)

仅 Android 状态栏图标

android - 如何使用 phonegap 在状态栏上创建通知

java - Android 闹钟未触发

javascript - 触发android下载管理器Cordova

android - AlarmManager 没有启动 Activity(当它存在时)

android - 如何在特定时间 2 天后停止我的闹钟服务?