Search in sources :

Example 11 with Contact

use of dev.sagar.smsblocker.tech.beans.Contact in project SMSBlocker by sagarpawardev.

the class NotificationUtilSingleton method createSMSNotification.

/**
 * This Method Creates a Notification
 * @param context
 * @param sms
 */
public void createSMSNotification(Context context, SMS sms) {
    final String methodName = "createSMSNotification()";
    log.justEntered(methodName);
    /*String fromNo = sms.getAddress();
        String fromName = fromNo;
        try {
            fromName = ContactUtilSingleton.getInstance().getContactName(context, fromNo);
        } catch (ReadContactPermissionException e) {
            e.printStackTrace();
        }
        String text = sms.getBody();

        String groupKey = fromNo;
        int notificationId = getNotificationId();

        log.info(methodName, "Creating Notification...");

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
                .setSmallIcon(R.drawable.ic_notif)
                .setContentTitle(fromName)
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setContentText(text)
                .setDefaults(NotificationCompat.DEFAULT_ALL)
                .setGroup(groupKey)
                .setStyle(new NotificationCompat.BigTextStyle().bigText(text))
                .setWhen(sms.getDateTime())
                .setShowWhen(true);

        Intent resultIntent = new Intent(context, ThreadActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString(ThreadActivity.KEY_ADDRESS, fromNo);
        resultIntent.putExtras(bundle);
        PendingIntent resultPendingIntent =
                PendingIntent.getActivity(
                        context,
                        0,
                        resultIntent,
                        PendingIntent.FLAG_UPDATE_CURRENT
                );
        mBuilder.setContentIntent(resultPendingIntent)
                .setAutoCancel(true);

        NotificationManager mNotifyMgr =
                (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotifyMgr.notify(notificationId, mBuilder.build());
        log.info(methodName, "Notification Created with ID: "+notificationId);*/
    /*generateSingleNotification(context, sms);
        setSummaryNotification(context, sms);*/
    int notifId = getNotificationId();
    String address = sms.getAddress();
    Contact contact = ContactUtilSingleton.getInstance().getContactOrDefault(context, address);
    ;
    // -- Open Activity onClick Ends
    Intent resultIntent = new Intent(context, ThreadActivity.class);
    Bundle bundle = new Bundle();
    bundle.putString(ThreadActivity.KEY_ADDRESS, address);
    resultIntent.putExtras(bundle);
    PendingIntent resultPendingIntent = PendingIntent.getActivity(context, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    // -- Open Activity onClick Ends
    // Get Notification Builder
    NotificationCompat.Builder mBuilder = getNotifBuilder(context, contact, sms);
    NotificationCompat.Builder mGroupBuilder = getNotifBuilder(context, contact, sms).setGroupSummary(true);
    NotificationCompat.Action action = getDirectReplyAction(context, sms, notifId);
    // reply action from step b above
    mBuilder.addAction(action);
    mGroupBuilder.addAction(action);
    // Set Priority
    setPriority(context, mBuilder);
    setPriority(context, mGroupBuilder);
    // Group Notification
    setGroup(context, mBuilder, contact);
    setGroup(context, mGroupBuilder, contact);
    // Setting on click envent
    mBuilder.setContentIntent(resultPendingIntent).setAutoCancel(true);
    mGroupBuilder.setContentIntent(resultPendingIntent).setAutoCancel(true);
    log.info(methodName, "Creating Notification..");
    NotificationManagerCompat mNotificationManager = NotificationManagerCompat.from(context);
    Notification notification = mBuilder.build();
    Notification notifGroupSummary = mGroupBuilder.build();
    mNotificationManager.notify(notifId, notification);
    mNotificationManager.notify(SUMMARY_ID, notifGroupSummary);
    log.returning(methodName);
}
Also used : Bundle(android.os.Bundle) NotificationManagerCompat(android.support.v4.app.NotificationManagerCompat) NotificationCompat(android.support.v4.app.NotificationCompat) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) PendingIntent(android.app.PendingIntent) Notification(android.app.Notification) Contact(dev.sagar.smsblocker.tech.beans.Contact)

Aggregations

Contact (dev.sagar.smsblocker.tech.beans.Contact)11 ReadContactPermissionException (dev.sagar.smsblocker.tech.exceptions.ReadContactPermissionException)7 Uri (android.net.Uri)5 ContentResolver (android.content.ContentResolver)3 Cursor (android.database.Cursor)3 Intent (android.content.Intent)2 ArrayList (java.util.ArrayList)2 Notification (android.app.Notification)1 PendingIntent (android.app.PendingIntent)1 Typeface (android.graphics.Typeface)1 Bundle (android.os.Bundle)1 NotificationCompat (android.support.v4.app.NotificationCompat)1 NotificationManagerCompat (android.support.v4.app.NotificationManagerCompat)1 MenuItem (android.view.MenuItem)1 SMS (dev.sagar.smsblocker.tech.beans.SMS)1