簡體   English   中英

當應用程序在Android中運行或不運行時,將推送通知內容顯示到警報對話框中

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

我是Android新手。

目前,我已將GCM功能集成到我的Android應用程序中。 我從第三方服務器應用程序中得到了推送通知。

但現在我的問題是每當推送通知出現時,它會顯示在Notification Bar區域,當我點擊該通知時它會按預期消失。

但我想要的功能是,當用戶點擊通知欄中的推送通知時,它會顯示彈出窗口並在彈出窗口中顯示通知內容。

我希望這個功能是應用程序運行與否。

即如果應用程序未運行,則通過單擊通知,它將自動在應用程序的第一個活動上顯示警報。 如果應用程序已在運行,那么它將顯示應用程序當前活動的警告框。

目前我的申請有7項活動。

嘗試在android中使用Pending Intent,將activity作為Dialog主題。 該鏈接將幫助您如何使用待定意圖幫助

收到通知時,使用此代碼在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);      

}

如果您按照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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM