Opening activity after clicking push notification android

前端 未结 2 1400
[愿得一人]
[愿得一人] 2020-12-30 15:57

I am a huge noob to Android programming so sorry if this is a simple task. I pretty much followed the Vogella push notification tutorial for push notifications (http://www.v

相关标签:
2条回答
  • 2020-12-30 16:12

    if you want to open a website on notification click try this:

        public void createNotification(Context context, String payload) {
            NotificationManager notificationManager = (NotificationManager) context
                    .getSystemService(Context.NOTIFICATION_SERVICE);
            Notification notification = new Notification(R.drawable.ic_launcher,
                    "Message received", System.currentTimeMillis());
            // Hide the notification after its selected
            notification.flags |= Notification.FLAG_AUTO_CANCEL;
    
            //adding LED lights to notification
            notification.defaults |= Notification.DEFAULT_LIGHTS;
    
            Intent intent = new Intent("android.intent.action.VIEW", 
             Uri.parse("http://my.example.com/"));
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
                    intent, 0);
            notification.setLatestEventInfo(context, "Message",
                    "New message received", pendingIntent);
            notificationManager.notify(0, notification);
    
        }
    
    0 讨论(0)
  • 2020-12-30 16:24

    In your base receiver for c2dm or the class the that extentd base reciever you have a handleMessage()::

    Below is the sample code for handle message which launches the activity::

    @Override
        protected void handleMessage(Context context, Intent intent) {
            String regId = C2DMessaging.getRegistrationId(context);
            String logKey = this.getClass().getSimpleName();
            String title="";
            String message="";
            if (regId!= null) {
                if (intent.hasExtra(Constants.TITLE)) {
                    title = intent.getStringExtra(Constants.TITLE);
                }
                if(intent.hasExtra(Constants.MESSAGE)){
                    message = intent.getStringExtra(Constants.MESSAGE);
                }
                // TODO Send this to my application server to get the real data
                // Lets make something visible to show that we received the message
                if(!title.equals("") && !message.equals(""))
                    createNotificationForMsg(context,title,message);
            }
        }
    
        @Override
        public void createNotificationForMsg(Context context,String title,String message) {
            final String logKey = this.getClass().getSimpleName();
    
            try {
                NotificationManager notificationManager = (NotificationManager) context
                        .getSystemService(Context.NOTIFICATION_SERVICE);
                Notification notification = new Notification(R.drawable.icon,
                        "update(s) received", System.currentTimeMillis());
                // Hide the notification after its selected
                notification.flags |= Notification.FLAG_AUTO_CANCEL;
                //adding sound to notification
                notification.defaults |= Notification.DEFAULT_SOUND;            
    
                    Intent intent = new Intent(context, YourAlertActivity.class);
    
                    if(Constants.LOG)Log.d(logKey, Constants.TITLE +": "+ title +" , "+Constants.MESSAGE+": "+message);
                    intent.putExtra(Constants.TITLE, title);
                    intent.putExtra(Constants.MESSAGE, message);
    
                    PendingIntent pendingIntent = PendingIntent.getActivity(context, Calendar.getInstance().get(Calendar.MILLISECOND),  intent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
                    notification.setLatestEventInfo(context, "Castrol",
                            title+"update Received", pendingIntent);
                    notificationManager.notify(Calendar.getInstance().get(Calendar.MILLISECOND), notification);
    
    
    
            } catch (Exception e) {
    //          MessageReceivedActivity.view.setText("createNotificationFor Msg: "
    //                  + e.toString());
            }
        }
    
    0 讨论(0)
提交回复
热议问题