How to create notification inside an activity in android?

808 Views Asked by At

I have a task in which I have to show Notification for an incoming message inside an activity using BroadcastReceiver with the address and content on the SMS. Now I have to click in order to insert the values in sms in database. When the task is finished i want it to clear from the activity. Is it possible to create notification for incoming sms inside an Activity?

1

There are 1 best solutions below

1
On BEST ANSWER

You can Try This no generate your own Notification.

@SuppressWarnings("deprecation")
private static void generateNotification(Context context) {

    int icon = R.drawable.ic_launcher;
    long when = System.currentTimeMillis();
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(icon, "New Contact Added",
            when);

    String title = context.getString(R.string.app_name);

    Intent notificationIntent = new Intent(context, MainActivity.class);

    // set intent so it does not start a new activity
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
            | Intent.FLAG_ACTIVITY_SINGLE_TOP);

    int iUniqueId = (int) (System.currentTimeMillis() & 0xfffffff);

    PendingIntent intent = PendingIntent.getActivity(context, iUniqueId,
            notificationIntent, 0);
    notification.setLatestEventInfo(context, title, "New", intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    // Play default notification sound
    notification.defaults |= Notification.DEFAULT_SOUND;

    // Vibrate if vibrate is enabled
    notification.defaults |= Notification.DEFAULT_VIBRATE;
    notificationManager.notify(2, notification);

}