[英]android notification for sms app
我正在創建短信應用,我希望每個電話號碼都具有差異通知。 此外,每個通知都應在味精中顯示未讀味精計數。 我正在使用此代碼,但是它將刪除舊的通知,並將其替換為新的通知。
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。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.