【问题标题】:To Show Push Notification content into the Alert Dialog when application is either running or not in android当应用程序在 android 中运行或未运行时,在警报对话框中显示推送通知内容
【发布时间】:2013-08-19 06:53:19
【问题描述】:

我是 Android 新手。

目前,我已将 GCM 功能集成到我的 android 应用程序中。我从我的第 3 方服务器应用程序中很好地收到了推送通知。

但现在我的问题是,每当推送通知出现时,它都会显示在通知栏区域中,当我单击该通知时,它会按预期消失。

但我想要这样的功能,当用户点击推送通知进入通知栏时,它会显示一个弹出窗口并将通知内容显示到该弹出窗口中。

无论应用程序是否正在运行,我都想要这个功能。

即如果应用程序未运行,则通过单击通知,它将自动在应用程序的第一个活动上显示警报。 如果应用程序已经在运行,那么它将在应用程序的当前活动中显示警报框。

目前我的应用程序有 7 个活动。

【问题讨论】:

  • 您需要创建一个主题为对话框的活动,并设置通知待处理意图以打开此对话框活动。

标签: android push-notification android-alertdialog google-cloud-messaging


【解决方案1】:

尝试在 android 中使用 Pending Intent 并将活动作为 Dialog 主题。该链接将帮助您如何使用待处理的意图help

【讨论】:

  • 感谢您的回复...:)
【解决方案2】:

当您收到通知时,使用此代码在 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);      

}

【讨论】:

    【解决方案3】:

    如果您按照 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());
    }
    

    【讨论】:

      猜你喜欢
      • 2013-03-12
      • 2015-09-15
      • 1970-01-01
      • 2011-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多