Detect incoming email notification in android

2020-04-18 08:22发布

问题:

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

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

Thanks

回答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



回答2:

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);
}
} 


回答3:

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>


回答4:

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



回答5:

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

reffer the following class

accessibility class