Android通知短信应用程序



我正在创建短信应用程序,我想对每个电话号码有diff通知。此外,每个通知应该在消息中显示未读消息计数。我正在使用此代码,但它删除了旧的通知,并将其替换为新的。

public class SmsReceiver extends BroadcastReceiver {
ArrayList<String> your_array_list = new ArrayList<String>();
@SuppressLint("NewApi")
@Override
public void onReceive(Context context, Intent intent) {
    // ---get the SMS message passed in---
    Bundle bundle = intent.getExtras();
    SmsMessage[] msgs = null;
    String str = "";
    String str2 = "";
    if (bundle != null) {
        // ---retrieve the SMS message received---
        Object[] pdus = (Object[]) bundle.get("pdus");
        msgs = new SmsMessage[pdus.length];
        for (int i = 0; i < msgs.length; i++) {
            msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
            str2 += msgs[i].getOriginatingAddress();
            // str2 += " :";
            str += msgs[i].getMessageBody().toString();
        }
        Uri lookupUri = Uri.withAppendedPath(
                PhoneLookup.CONTENT_FILTER_URI, Uri.encode(str2));
        Cursor c = context.getContentResolver().query(lookupUri,
                new String[] { ContactsContract.Data.DISPLAY_NAME }, null,
                null, null);
        try {
            c.moveToFirst();
            String displayName = c.getString(0);
            str2 = displayName;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            c.close();
        }
        Intent i = new Intent(context, MainActivity.class);
        PendingIntent pIntent = PendingIntent.getActivity(context, 0, i, 0);
        String uri = "tel:" + str2;
        Intent iph = new Intent(Intent.ACTION_CALL, Uri.parse(uri));
        PendingIntent pCall = PendingIntent.getActivity(context, 0, iph, 0);
        // Build notification
        Notification noti = new Notification.Builder(context)
                .setStyle(new Notification.BigTextStyle().bigText(str))
                .setContentTitle("New sms from " + str2)
                .setContentText(str)
                .setSmallIcon(R.drawable.ic_action_chat)
                .setContentIntent(pIntent)
                .addAction(R.drawable.ic_action_call, "Call", pCall)
                .addAction(R.drawable.ic_action_accept, "Read", pIntent)
                .addAction(R.drawable.ic_action_discard, "Delete", pIntent)
                .build();
        NotificationManager notificationManager = (NotificationManager) context
                .getSystemService(context.NOTIFICATION_SERVICE);
        // hide the notification after its selected
        noti.flags |= Notification.FLAG_AUTO_CANCEL;
        // notification
        notificationManager.notify(0, noti);
    }
}

}

那么我如何修复这个并显示通知计数。此外,当我点击一些动作通知不会消失。如何解决这个问题。谢谢你

notificationManager.notify(0, noti);

您放在这里的0是通知的id。因此,如果您对每个通知使用0,则通知将不断被替换。我们的想法是为每个通知使用不同的id。

最新更新