簡體   English   中英

當設備收到GCM推送通知時,會彈出Android應用程序

[英]Android app pop up when a GCM push notification is received on the device

我想在接收來自服務器的gcm通知或消息時實現一個應用程序彈出。 通常,服務器向GCM api發送消息,並從那里將消息發送給客戶端。

在客戶端通常您會收到通知,但我不希望我希望客戶端設備激活或打開或啟動該特定應用程序。

可能嗎? 如果是這樣的話?

以下是我的通知生成功能

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

        Intent notificationIntent = new Intent(context, MainActivity.class);
        // set intent so it does not start a new activity
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
                Intent.FLAG_ACTIVITY_SINGLE_TOP);
        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;

        // Vibrate if vibrate is enabled
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        notificationManager.notify(0, notification);    
    }

如果這個問題已經被問到某個地方,那么請重新定向我並刪除我的問題。

提前致謝。

如果我理解正確,你想要的是直接啟動應用程序而不是創建通知。

為此,您只需要使用以下代碼替換對generateNotification()的調用:

Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("your.package.name");
startActivity(LaunchIntent);

這就是你需要的嗎?

如果您不想收到通知,那么您的代碼不應該創建通知。 而不是調用generateNotification調用一個方法來啟動您的應用程序的主要Activity (或您希望您的應用程序開始的任何Activity )。

    Intent intent = new Intent().setClass(context, MainActivity.class);
    startActivity(intent);  

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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