1

Can we detect a notification when there is incoming email in android ?

is there any solution, tutorial, or sample code I can try?

Thanks

Sodiq
  • 316
  • 2
  • 13

5 Answers5

1

Try to implements NotificationListenerService. here is the official documentation https://developer.android.com/reference/android/service/notification/NotificationListenerService.html

and you can take a look to this question NotificationListenerService Implementation

Community
  • 1
  • 1
Basbous
  • 3,927
  • 4
  • 34
  • 62
  • NotificationListenerService can detect incoming email from gmail application? Thanks for your link – Sodiq Dec 18 '14 at 09:11
  • NotificationManager will not have complete content of the mail, what if in case i wanted to readout the entire mail content?is it possible to fetch complete mail content from notificationmanager? – siva Feb 25 '16 at 12:10
  • That would be a serious privacy issue. Tell me, would you feel comfortable with apps actually READING your e-mail? – Rens Verhage Dec 04 '16 at 15:30
1

You want below api Jelly bean you should use the accessibility service

reffer the following class

accessibility class

hareesh J
  • 520
  • 1
  • 6
  • 21
1

I think that you searching for BroadcastReceiver (only if you manage the email by yourself and it is not a third-party email. In this case, probably you can do nothing):

http://www.vogella.com/tutorials/AndroidBroadcastReceiver/article.html

Mou
  • 2,027
  • 1
  • 18
  • 29
1

you should implement a broadcast receiver and listen to "android.intent.action.PROVIDER_CHANGED" intent

<receiver android:name="GmailReceiver">
        <intent-filter>
            <action android:name="android.intent.action.PROVIDER_CHANGED"
                android:priority="-10">
            </action>
            <action android:name="android.intent.action.VIEW" />
            <data android:scheme="content" android:host="gmail-ls"
                android:pathPattern="/unread/.*">
            </data>
        </intent-filter>
    </receiver>
Boris Gafurov
  • 1,427
  • 16
  • 28
gor
  • 1,046
  • 1
  • 14
  • 28
1

gor's answer worked (i edited it a bit)!!! thanks.

So add that to your manifest. And here is the receiver class I used to add notification to my app:

public class GmailReceiver extends BroadcastReceiver{

Context cntxt;

@Override
public void onReceive(Context context, Intent intent) {
    Toast.makeText(context, "Email Received", Toast.LENGTH_LONG).show();
   showNotification(context);
}

private void showNotification(Context context) {

    Intent notificationIntent = new Intent(context, YOUR_ACTIVITY_HERE.class);
    PendingIntent contentIntent = PendingIntent.getActivity(context,
            0, notificationIntent,
            PendingIntent.FLAG_CANCEL_CURRENT);

    NotificationManager nm = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);

    Resources res = context.getResources();
    Notification.Builder builder = new Notification.Builder(context);

    builder.setContentIntent(contentIntent)
            .setSmallIcon(R.drawable.YOUR_APP_icon)
            .setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.YOUR_APP_icon))
            .setTicker(res.getString(R.string.app_name))
            .setWhen(System.currentTimeMillis())
            .setAutoCancel(true)
            .setContentTitle(res.getString(R.string.app_name))
            .setContentText(res.getString(R.string.app_name));
    Notification n = builder.getNotification();
    nm.notify(1, n);
}
} 
Boris Gafurov
  • 1,427
  • 16
  • 28