简体   繁体   English

当应用程序在Android中运行或不运行时,将推送通知内容显示到警报对话框中

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

I am new for the Android. 我是Android新手。

Currently, I have integrated GCM Functionality into the my android application. 目前,我已将GCM功能集成到我的Android应用程序中。 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. 但现在我的问题是每当推送通知出现时,它会显示在Notification Bar区域,当我点击该通知时它会按预期消失。

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. 目前我的申请有7项活动。

try Using Pending Intent in android with activity as Dialog theme . 尝试在android中使用Pending Intent,将activity作为Dialog主题。 the link will help u how to use pending intents help 该链接将帮助您如何使用待定意图帮助

use this code to generate notification in GCMIntentService when you receive notification 收到通知时,使用此代码在GCMIntentService中生成通知

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: 如果您按照GCM使用MyGcmListenerService,那么您的代码应该类似于:

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());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从parse.com控制台收到推送时显示警报对话框而不是通知 - Show alert dialog instead of notification when recieve push from parse.com console 仅在应用未运行android时显示推送通知 - Show push notification only when app is not running android 当 android 应用程序收到通知时如何显示自定义对话框 - how to show custom dialog box when android application receives notification 即使应用程序未运行,也会显示警报对话框 - Alert dialog display even when the application is not running 当应用未运行时,Android设备未响应远程推送通知而显示警报 - Android device not showing alert in response to remote push notification when the app is not running android服务在启动应用程序时显示推送通知 - android service to show push notification on launch of an application 在应用程序运行时如何在通知栏中显示“解析推送通知”? - How to show Parse Push Notification in notification bar while application is running? 按下通知时如何显示后台FCM消息的警报对话框 - How to show an Alert Dialog for a background FCM message when notification is pressed 当应用程序在前台运行时,在警告框中显示推送通知 - Display push notification in alert box when the app is running in the foreground android - 如何在单击通知时显示对​​话框 - android - How to show a dialog when click on notification
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM