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()); 
} 
相關問題