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

后端 未结 2 1840
离开以前
离开以前 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

    0 讨论(0)
  • 2021-01-03 10:07

    onMessage method called every time when you got any notification. For opening a dialog box on notification you can start an another activity in onMessage and put your code (for dialog box) in that Activity.Or you can also make it notification. Like this:-

    public class GCMIntentService extends GCMBaseIntentService
    {
       @Override
       protected void onMessage( Context myContext, Intent intent) 
       {
            <your code>
            //if you want to show any dialog directly.
            Intent i = new Intent(myContext,<your Activity.class>);
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
            i.putExtra("message", message);
            myContext.startActivity(i); 
    
          //if you want to show message through notification.
          NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
          Notification notification = new Notification(R.drawable.notification_icon,arg1.getStringExtra("message"), when);
          String title = context.getString(R.string.app_name);
          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);
          PendingIntent intent = PendingIntent.getActivity(context, 0,notificationIntent, 0);
          notification.setLatestEventInfo(context, title, arg1.getStringExtra("message"), intent);
          notification.flags |= Notification.FLAG_AUTO_CANCEL;
          notificationManager.notify(0, notification);
      }
    }
    

    Please let me know if this helps you and Marry Christmas :-).

    0 讨论(0)
提交回复
热议问题