循环通过android sqlite数据库

时间:2015-10-31 14:18:24

标签: android push-notification cursor google-cloud-messaging android-sqlite

我正在尝试将Whatsapp / Email通知样式添加到我的应用程序中,当有一个通知出现而另一个通知出现时,它们都显示在同一条消息上。我这样做是通过将通知保存到数据库中,该数据库在单击消息时被删除。目前,我的循环效果很好,除非我发送超过2个通知,例如4。 [![工作时] [1]] [1]

当它破裂时..

[![图片] [2] [2]

这些数字代表我发送通知的顺序。正如你所看到的,Four重复了两次而不是没有..截至上下截屏二的所需顺序是Four-> Three-> Two-> One。

这里是循环代码..

    Cursor cur;
    ............
    ............
    int imsg = cur.getColumnIndex(KEY_MSG);
    int ititle = cur.getColumnIndex(KEY_TITLE);

    NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
    inboxStyle.setBigContentTitle(makeNotificationLine(title, ""));

    if (c == 0) {
        //when DB is empty...
        inboxStyle.addLine(msg);
    } else {

        for (cur.moveToFirst(); !cur.isAfterLast(); cur.moveToNext()) {
            num++;
            inboxStyle.setBigContentTitle("Reelforge");
            inboxStyle.addLine(makeNotificationLine(title, msg));
            inboxStyle.addLine(makeNotificationLine(cur.getString(ititle), cur.getString(imsg)));
        }
    }

    inboxStyle.setSummaryText(num + " new notifications");
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle(title)
            .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS)
            .setContentText(msg);
        mBuilder.setPriority(NotificationCompat.PRIORITY_DEFAULT);
        mBuilder.setAutoCancel(true);
        mBuilder.setStyle(inboxStyle);
        mBuilder.setContentIntent(contentIntent);

        the_db.close();
        Random random = new Random();
        int m = random.nextInt(9999 - 1000) + 1000;
        String x = m + "";

        SimpleDateFormat sdf = new SimpleDateFormat("LLL d, yyyy");
        sdf.setTimeZone(TimeZone.getDefault());
        String date = sdf.format(new Date());

        try {
            the_db.open();
            the_db.createmsgEntry(x, title, msg);
            the_db.createmsgEntry2(x, title, msg, date);
            the_db.close();

        } catch (Exception e) {
            the_db.close();
        } finally {
            if (the_db != null) {
                the_db.close();
            }
        }

        mNotificationManager.notify(N_ID, mBuilder.build());



public long createmsgEntry(String s2, String s3, String s4) {

    ContentValues cv = new ContentValues();
    cv.put(KEY_MSGID, s2);
    cv.put(KEY_TITLE, s3);
    cv.put(KEY_MSG, s4);

    return ourDatabase.insert(DATABASE_TABLE, null, cv);

}

2 个答案:

答案 0 :(得分:1)

终于解决了......

mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    int num = 0;
    Random random = new Random();
    int m = random.nextInt(9999 - 1000) + 1000;

    String x = m + "";

    SimpleDateFormat sdf = new SimpleDateFormat("LLL d, yyyy");
    sdf.setTimeZone(TimeZone.getDefault());
    String date = sdf.format(new Date());

    Intent i = new Intent(this, MainActivity.class);

    i.setAction("gcm");

    //opened after clicking
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, i, 0);

    try {
        the_db.open();
        c = the_db.getCount();
        cur = the_db.getmsgData();
        the_db.createmsgEntry(x, title, msg);
        the_db.createmsgEntry2(x, title, msg, date);

    } catch (Exception e) {
        the_db.close();
    }

    int imsg = cur.getColumnIndex(KEY_MSG);
    int ititle = cur.getColumnIndex(KEY_TITLE);

    NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
    inboxStyle.setBigContentTitle(makeNotificationLine(title, ""));

    if (c == 0) {
        num++;
        inboxStyle.addLine(msg);
    } else {
        for (cur.moveToFirst(); !cur.isAfterLast(); cur.moveToNext()) {
            num++;
            inboxStyle.setBigContentTitle("New Notifications");
            inboxStyle.addLine(makeNotificationLine(cur.getString(ititle), cur.getString(imsg)));
        }
    }

    inboxStyle.setSummaryText(num + " new notifications");

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(title)
            .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS)
            .setContentText(msg);
    mBuilder.setPriority(NotificationCompat.PRIORITY_DEFAULT);
    mBuilder.setAutoCancel(true);
    mBuilder.setStyle(inboxStyle);
    mBuilder.setContentIntent(contentIntent);
    the_db.close();

    mNotificationManager.notify(N_ID, mBuilder.build());

答案 1 :(得分:0)

如果您查看此处的文档Applying an expanded layout to a notification

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
    .../
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();

// Set up inboxStyle
inboxStyle.setBigContentTitle(.../
    ...
// Moves events into the expanded layout
for (int i=0; i < events.length; i++) {

    inboxStyle.addLine(events[i]);
}
// Moves the expanded layout object into the notification object.
mBuilder.setStyle(inBoxStyle);
    ...
// Issue the notification here.

创建构建器后添加行。 首先:照顾你的数据库;

// To do.. add new notifications to db, parse values etc,

然后创建新构建器并遍历数据库以获取要添加到行的值。

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
        ..../
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
inboxStyle.setBigContentTitle(makeNotificationLine(title, ""));

if (c == 0) {
    //when DB is empty...
    inboxStyle.addLine(msg);
} else {

    for (cur.moveToFirst(); !cur.isAfterLast(); cur.moveToNext()) {
        num++;
        inboxStyle.setBigContentTitle("Reelforge");
        inboxStyle.addLine(makeNotificationLine(title, msg));
        inboxStyle.addLine(makeNotificationLine(cur.getString(ititle), cur.getString(imsg)));
    }
}

inboxStyle.setSummaryText(num + " new notifications");

 mNotificationManager.notify(N_ID, mBuilder.build());

每次更新此类通知时,都需要实际创建新的通知构建器。您无法向现有通知构建器添加行。你可以更新一些东西,比如标题。

一些很好的链接。

Update android notification with new line is removing previous lines

Android multiple line notification like Gmail app

Update text of notification, not entire notification