java - 如何创建多个通知?

标签 java android android-layout notifications

这是我的 MyReceiver.java 类,我在这里编写了一段代码来显示单个通知。如何在此处创建多个通知。

         public class MyReceiver extends BroadcastReceiver{
String Reqpopending;
int MID=0;
@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub


    long when = System.currentTimeMillis();
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);

    Intent notificationIntent = new Intent(context, SplashScreen.class);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
            notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);


    Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(
            context).setSmallIcon(R.drawable.dashboard)
            .setContentTitle("Dash Board Counts")
            .setContentText("P.O Counts-532,198,654,255,901 ZeroInward-303 PartialInward-777").setSound(alarmSound)
            .setAutoCancel(true).setWhen(when)
            .setContentIntent(pendingIntent)
            .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
    notificationManager.notify(MID, mNotifyBuilder.build());


    MID++;

}

最佳答案

请在下面找到正在循环中运行的代码

public class MyReceiver extends BroadcastReceiver{
String Reqpopending;
int MID=0;
@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub

    for(int i = 0; i < 3; i++)
    {
    long when = System.currentTimeMillis();
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);

    Intent notificationIntent = new Intent(context, SplashScreen.class);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
            notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);


    Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(
            context).setSmallIcon(R.drawable.dashboard)
            .setContentTitle("Dash Board Counts")
            .setContentText("P.O Counts-532,198,654,255,901 ZeroInward-303 PartialInward-777").setSound(alarmSound)
            .setAutoCancel(true).setWhen(when)
            .setContentIntent(pendingIntent)
            .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
    notificationManager.notify((int)when, mNotifyBuilder.build());
    }

希望这对您有所帮助。

关于java - 如何创建多个通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35030993/

相关文章:

java - Cookie 值中存在无效字符 [59]

java - 显示 NatTable 上下文菜单

java - "incompatible datatypes in combination"查询日期值时出错

java - TreeSet操作的复杂性

java - 解析android中的JSON响应 "response of type org.json.JSONObject cannot be converted to JSONArray"

android - 在显示警报对话框时暂停进一步执行

java - 使用导出的 sklearn 模型时 Android Studio 中的 "Code too large"

Android WebView 安全字体系列?

java - 在 Parse 上检索文件 - 图像

android - Scrollview -> Relativelayout -> TabLayout 不要滚动整个布局