I can't open a dialog from GCM onMessage in Android

后端 未结 2 1839
离开以前
离开以前 2021-01-03 09:33

When sending a message to my android app using google cloud messaging I can\'t figure out how to open a yes or no dialog (like a javasrcript confirm box) that opens a websit

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-03 10:02

    Try this working, In GCMIntentService write the above code in generate notification

    private static void generateNotification(Context context, String message) {
            long when = System.currentTimeMillis();
            NotificationManager notificationManager = (NotificationManager) context
                    .getSystemService(Context.NOTIFICATION_SERVICE);
            Notification notification = new Notification(R.drawable.appicon,
                    message, when);
            String title = context.getString(R.string.app_name);
            Intent notificationIntent = new Intent(context,
                    YourClassName.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;
            notificationManager.notify(0, notification);
        }
    

    when click the notification it goes to the activity in your application, after that in the activity you may create the dialog-box and whatever you want put inside your activity

提交回复
热议问题