Sms收到时,联系人名称未显示在通知中

时间:2016-05-12 08:24:10

标签: android push-notification broadcastreceiver title

我正在创建一个应用程序,当新的SMS从已知号码收到时,即如果号码保存在联系人列表中,则推送通知显示。现在问题是通知没有显示联系人的显示名称。它可以显示消息编号和消息内容,但不显示该联系人的显示名称。

检查新短信是否来自保存的联系人的代码是:

public boolean contactExists(Context context, String number) {
Uri lookupUri = Uri.withAppendedPath(
PhoneLookup.CONTENT_FILTER_URI, 
Uri.encode(number));
String[] mPhoneNumberProjection = { PhoneLookup._ID, PhoneLookup.NUMBER, PhoneLookup.DISPLAY_NAME };
Cursor cur = context.getContentResolver().query(lookupUri,mPhoneNumberProjection, null, null, null);
try {
   if (cur.moveToFirst()) {
      return true;
   }
} 
finally 
{
    if (cur != null)
   cur.close();
}
return false;
}

和通知警报的代码是:

if (contactExists(context, msg_from))
        {

            NotificationCompat.Builder notify = new NotificationCompat.Builder(context);
            notify.setSmallIcon(R.drawable.appicon);
            notify.setContentTitle(msg_from);
            notify.setContentText(msgBody);
            notify.setAutoCancel(true);
            notify.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
            notify.setLights(Color.GREEN, 2000, 2000);
            notify.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
            Intent notificationIntent = new Intent(Intent.ACTION_MAIN);
            notificationIntent.addCategory(Intent.CATEGORY_DEFAULT);
            notificationIntent.setType("vnd.android-dir/mms-sms");
            notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            PendingIntent intentt = PendingIntent.getActivity(context, 0,notificationIntent, 0);
            notify.setContentIntent(intentt);
            //notify.setContentIntent(PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 268435456));
            NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
            notificationManager.notify(0, notify.build());

        }

现在正如你在这一行中看到的那样,我写了#34; msg_from"

notify.setContentTitle(msg_from);

这会给我一个消息来自哪里的号码,但如果我写这个:

notify.setContentTitle(PhoneLookup.DISPLAY_NAME);

或我在互联网上找到的其他解决方案然后显示的通知标题将显示此字符串" display_name"并没有显示实际名称 请帮助!

这是该类的所有代码

public class TodoRe extends BroadcastReceiver {
Context context;

ArrayList<String> keywordslist = new ArrayList<String>();


@SuppressLint({ "DefaultLocale", "InlinedApi" })
@Override
public void onReceive(Context context, Intent intent) {

LinkedHashMap<String, String> contactNumber = new LinkedHashMap<String, String>();
DBkeyword screenedKeywordDB = new DBkeyword(context);
SQLiteDatabase db = screenedKeywordDB.getWritableDatabase();
Cursor cur1 = db.rawQuery(Constants.readScreenedKeywords, null);
cur1.moveToFirst();
while (!cur1.isAfterLast()) 
{
    keywordslist.add(cur1.getString(0));
    cur1.moveToNext();
}

db.close();
DBTable dbtable = new DBTable(context);
SQLiteDatabase dbrsn = dbtable.getReadableDatabase();
Cursor cur = dbrsn.rawQuery(Constants.readScreenedNumbers, null);
cur.moveToFirst();

while (!cur.isAfterLast()) 
{
  contactNumber.put(cur.getString(0), cur.getString(1));
  cur.moveToNext();
}
if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
  Bundle bundle = intent.getExtras();
  SmsMessage[] msgs = null;
  String msg_from,msgSenderName;
  if (bundle != null) {
    try {
      boolean keywordPresent = false;
      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]);
        msg_from = msgs[i].getOriginatingAddress();

        String msgBody = msgs[i].getMessageBody();
        String title= cur.getString(cur.getColumnIndex(PhoneLookup.DISPLAY_NAME));
        msg_from = Utilities.extractNumbers(msg_from);
        Long dateLong = msgs[i].getTimestampMillis();
        String msgDate = dateLong.toString();

        ContentValues values = new ContentValues(); 
        values.put("address", msg_from); 
        values.put("date", System.currentTimeMillis()+""); 
        values.put("read", "1"); 
        values.put("type", "1"); 
        values.put("body",msgBody); 
        Uri uri = Uri.parse("content://sms/"); 
        context.getContentResolver().insert(uri,values);

        if (contactExists(context, msg_from))
        {
            NotificationCompat.Builder notify = new NotificationCompat.Builder(context);
            notify.setSmallIcon(R.drawable.appicon);
            notify.setContentTitle(title);
            notify.setContentText(msgBody);
            notify.setAutoCancel(true);
            notify.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
            notify.setLights(Color.GREEN, 2000, 2000);
              notify.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
            Intent notificationIntent = new Intent(Intent.ACTION_MAIN);
            notificationIntent.addCategory(Intent.CATEGORY_DEFAULT);
            notificationIntent.setType("vnd.android-dir/mms-sms");
            notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            PendingIntent intentt = PendingIntent.getActivity(context, 0,notificationIntent, 0);
            notify.setContentIntent(intentt);
            //notify.setContentIntent(PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 268435456));
            NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
            notificationManager.notify(0, notify.build());

        }


public boolean contactExists(Context context, String number) {
Uri lookupUri = Uri.withAppendedPath(
PhoneLookup.CONTENT_FILTER_URI, 
Uri.encode(number));
String[] mPhoneNumberProjection = { PhoneLookup._ID, PhoneLookup.NUMBER, PhoneLookup.DISPLAY_NAME };
Cursor cur = context.getContentResolver().query(lookupUri,mPhoneNumberProjection, null, null, null);
try {
   if (cur.moveToFirst()) {
      return true;
   }
} 
finally 
{
    if (cur != null)
   cur.close();
}
return false;
}

以下是现在更新的代码

ContentValues values = new ContentValues(); 
        values.put("address", msg_from); 
        values.put("date", System.currentTimeMillis()+""); 
        values.put("read", "1"); 
        values.put("type", "1"); 
        values.put("body",msgBody); 
        Uri uri = Uri.parse("content://sms/"); 
        context.getContentResolver().insert(uri,values);
        String title = contactName(context, msg_from);
        String sss=cur.getString(cur.getColumnIndex(PhoneLookup.DISPLAY_NAME));
        if (contactExists(context, msg_from) && title != null)
        {
            NotificationCompat.Builder notify = new NotificationCompat.Builder(context);
            notify.setSmallIcon(R.drawable.appicon);
            notify.setContentTitle(sss);
            notify.setContentText(msgBody);
            notify.setAutoCancel(true);
            notify.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
            notify.setLights(Color.GREEN, 2000, 2000);
            notify.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
            Intent notificationIntent = new Intent(Intent.ACTION_MAIN);
            notificationIntent.addCategory(Intent.CATEGORY_DEFAULT);
            notificationIntent.setType("vnd.android-dir/mms-sms");
            notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            PendingIntent intentt = PendingIntent.getActivity(context, 0,notificationIntent, 0);
            notify.setContentIntent(intentt);
            //notify.setContentIntent(PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 268435456));
            NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
            notificationManager.notify(0, notify.build());

        }

public boolean contactExists(Context context, String number) {
Uri lookupUri = Uri.withAppendedPath(
PhoneLookup.CONTENT_FILTER_URI, 
Uri.encode(number));
String[] mPhoneNumberProjection = { PhoneLookup._ID, PhoneLookup.NUMBER, PhoneLookup.DISPLAY_NAME };
Cursor cur = context.getContentResolver().query(lookupUri,mPhoneNumberProjection, null, null, null);
try {
   if (cur.moveToFirst()) {
      return true;
   }
} 
finally 
{
    if (cur != null)
   cur.close();
}
return false;
}

public String contactName(Context context, String number) {
Uri lookupUri = Uri.withAppendedPath(
PhoneLookup.CONTENT_FILTER_URI, 
Uri.encode(number));
String[] mPhoneNumberProjection = { PhoneLookup._ID, PhoneLookup.NUMBER, PhoneLookup.DISPLAY_NAME };
Cursor cur = context.getContentResolver().query(lookupUri,mPhoneNumberProjection, null, null, null);
try {
   if (cur.moveToFirst()) {
       return cur.getString(cur.getColumnIndex(PhoneLookup.DISPLAY_NAME ));
   }
} 
finally 
{
    if (cur != null)
   cur.close();
}
return null;
}

现在有两种方法,一种是检查是否存在,另一种是返回名称

1 个答案:

答案 0 :(得分:1)

DISPLAY_NAME常量是包含联系人显示名称的数据库表格列的名称。它的值为"display_name",这就是您在Notification中看到的原因。

您需要Cursor对该列的值。也就是说,你想要:

cur.getString(cur.getColumnIndex(PhoneLookup.DISPLAY_NAME))

更改contactExists()方法,为显示名称返回String,而不是仅指示是否存在的boolean

public String contactExists(Context context, String number) {
    ...

    try {
        if (cur.moveToFirst()) {
            return cur.getString(cur.getColumnIndex(PhoneLookup.DISPLAY_NAME ));
        }
    } 
    finally {
        if (cur != null)
            cur.close();
    }
    return null;
}

然后,将title更改为该方法的返回值,并更改if语句以检查title是否不是null

String title = contactExists(context, msg_from);

if (title != null) {
    NotificationCompat.Builder notify = ...
    ...
}

您可能还想更改contactExists()方法名称,因为它现在返回显示名称。