1

中时,向推送对话框显示推送通知内容我是Android新手。当应用程序正在运行或不在android

目前,我已将GCM功能集成到我的android应用程序中。我从我的第三方服务器应用程序中获得了推送通知。

但现在我的问题是,每当推送通知到来时,它会显示在通知栏区域,当我点击该通知时,它就会像预期的那样消失。

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

我想要这个功能,或者应用程序正在运行或不运行。

即如果应用程序没有运行,那么通过点击通知它会自动显示应用程序第一个活动的警报。 如果应用程序已经在运行,那么它会在应用程序的当前活动中显示警告框。

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

+0

您需要创建一个主题为活动的活动作为对话框,并设置通知等待意图打开此对话框活动。 – Tarun

回答

1

尝试使用Android中的Pending Intent作为对话主题的活动。该链接将帮助ü如何使用挂起的意图help

+0

感谢您的回复... :) –

1

使用此代码生成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);  

} 
1

如果您使用MyGcmListenerService按照GCM,那么你的代码应该是如:

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()); 
} 
相关问题