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
In order to implement this functionality ,first you need to establish the connection with the gmail server,then you need to check the inbox folder for new messages. If find then send the notification to the user using NotificationManager. please follow this links http://www.jondev.net/articles/Sending_Emails_without_User_Intervention_%28no_Intents%29_in_Android and another link is
Sending Email in Android using JavaMail API without using the default/built-in app
Try this:
Properties props = new Properties();
//IMAPS protocol
props.setProperty(“mail.store.protocol”, “imaps”);
//Set host address
props.setProperty(“mail.imaps.host”, imaps.gmail.com);
//Set specified port
props.setProperty(“mail.imaps.port”, “993″);
//Using SSL
props.setProperty(“mail.imaps.socketFactory.class”, “javax.net.ssl.SSLSocketFactory”);
props.setProperty(“mail.imaps.socketFactory.fallback”, “false”);
//Setting IMAP session
Session imapSession = Session.getInstance(props);
Store store = imapSession.getStore(“imaps”);
//Connect to server by sending username and password.
//Example mailServer = imap.gmail.com, username = abc, password = abc
store.connect(mailServer, account.username, account.password);
//Get all mails in Inbox Forlder
inbox = store.getFolder(“Inbox”);
inbox.open(Folder.READ_ONLY);
//Return result to array of message
Message[] result = inbox.getMessages();
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:
<service android:name="com.secondclone.UINotificationService"
android:label="@string/app_name_notification"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService"
/>
</intent-filter>
</service>
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");
}
}