How to receive email from gmail android

前端 未结 3 1646
再見小時候
再見小時候 2021-01-03 06:48

I am new to android programming.

I got my app with Gmail account sends emails. What I need now is how to receive new emails from G mail? Or at least how to get a not

3条回答
  •  梦毁少年i
    2021-01-03 07:27

    You need to first grant permission to "Notification accept " so your app can receive any notifications from device apps.

    You need to follow the steps below to enable the "Notification accept" permission:

    Setting => Apps => Special access => Notification accept

    You need to give your application permission in AndroidManifest.xml:

       
        
        
        
        
    

    Then, you write down the conditions to only receive notifications for new email notifications

    /* This is the class that helps you receive notifications when there are new emails */
    public class UINotificationService extends NotificationListenerService {
    
        @Override
        public void onCreate()
        {
            super.onCreate(); 
        }
    
    @Override
    
    public void onNotificationPosted(StatusBarNotification sbn)
    {
    
        // Get notification of new messages of the Gmail app com.google.android.gm
        if (sbn.getPackageName().equals("com.google.android.gm"))
        {
    
           /* What you need to handle when a new email is here */
           Bundle extras = sbn.getNotification().extras;
                    if (!contentGmail.equals(extras.getCharSequence("android.bigText").toString()))
                    {
                        contentGmail = Objects.requireNonNull(extras.getCharSequence("android.bigText")).toString();
    
                        // This is the recipient's Gmail name information.
                        String mreceiver = extras.getString("android.subText");
    
                        // This is the sender's name.
                        String mSender = extras.getString("android.title");
    
                        // This is the Email subject.
                        String mSubject = Objects.requireNonNull(extras.getCharSequence("android.text")).toString();
    
                        // This is the text of this new mail.
                        String mContent = Objects.requireNonNull(extras.getCharSequence("android.bigText")).toString();
    
                        //Notification.EXTRA_TEXT
                        time = sbn.getPostTime() / 1000;
                        Log.i("tsMail", "Sender = " + mSender + " Receiver= " + receiver + " Content Gmail= " + mContent );
    
                        }
                    }
    
        }
    }
    
    @Override
    public void onNotificationRemoved(StatusBarNotification sbn) {
        Log.i("Msg","Notification Removed");
        }
    }
    

提交回复
热议问题