简体   繁体   中英

To Show Push Notification content into the Alert Dialog when application is either running or not in android

I am new for the Android.

Currently, I have integrated GCM Functionality into the my android application. I got the push notification very well from my 3rd party-server application.

But now my problem is that whenever push notification is comes it shows into the Notification Bar area and when I click on that notification it simply disappear as expected.

But I want the functionality that when user clicks on the push notification into the notification bar it will shows a pop-up and shows notification content into that pop-up.

I want this functionality either the application is running or not.

ie If the application is not running, then by clicking the notification it will automatically shown the alert on the application's first activity. And if application is already running, then it will shows the alert box on the current activity of the application.

Currently my application has 7 activities.

try Using Pending Intent in android with activity as Dialog theme . the link will help u how to use pending intents help

use this code to generate notification in GCMIntentService when you receive notification

private static void generateNotification(Context context, String message) {
    int icon = R.drawable.ic_launcher;
    long when = System.currentTimeMillis();
    NotificationManager notificationManager = (NotificationManager)
            context.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(icon, message, when);

    String title = context.getString(R.string.app_name);
                                                 //activity which you want to open
    Intent notificationIntent = new Intent(context, YOUR_ACTIVITY.class);
    // set intent so it does not start a new activity
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
            Intent.FLAG_ACTIVITY_SINGLE_TOP);
   notificationIntent.putExtra("m", message);
    PendingIntent intent =
            PendingIntent.getActivity(context, 0, notificationIntent, 0);
    notification.setLatestEventInfo(context, title, message, intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;

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

    //notification.sound = Uri.parse("android.resource://" + context.getPackageName() + "your_sound_file_name.mp3");

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

}

If you're using MyGcmListenerService as per GCM, then your code should be something like:

private void sendNotification(String title, String body)
{
    Context context = getBaseContext();

    Intent notificationIntent = new Intent(context, <the-activity-you-need-to-call>.class);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
            .setSmallIcon(R.mipmap.ic_l)
            .setContentTitle(title)
            .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
            .setVibrate(new long[] { 1000, 1000})
            .setContentText(body)
            .setContentIntent(pendingIntent)
            .setAutoCancel(true);

    NotificationManager mNotificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);

    mNotificationManager.notify(MESSAGE_NOTIFICATION_ID, mBuilder.build());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM