Android:使用Gmail api从服务中读取Gmail消息正文

Android:使用Gmail api从服务中读取Gmail消息正文,android,gmail-api,android-alarms,incoming-mail,Android,Gmail Api,Android Alarms,Incoming Mail,我想要的是,当gmail通知到达时,我想从服务中读取gmail消息正文。当Gmail通知到达时,会出现警报,我会在alarmReceiver中获得全文 我在这里得到了android快速入门gsuits api:。但是这里只描述了Android Sdk和依赖项。我没有找到捕获gmail主体的整个过程 dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcom

我想要的是,当gmail通知到达时,我想从服务中读取gmail消息正文。当Gmail通知到达时,会出现警报,我会在alarmReceiver中获得全文

我在这里得到了android快速入门gsuits api:。但是这里只描述了Android Sdk和依赖项。我没有找到捕获gmail主体的整个过程

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:25.0.1'
compile 'com.google.android.gms:play-services-auth:15.0.1'
compile 'pub.devrel:easypermissions:0.3.0'
compile('com.google.api-client:google-api-client-android:1.23.0') {
    exclude group: 'org.apache.httpcomponents'
}
compile('com.google.apis:google-api-services-<API>-<VERSION>') {
    exclude group: 'org.apache.httpcomponents'
}}
依赖项{
编译文件树(目录:“libs”,包括:['*.jar'])
编译'com.android.support:appcompat-v7:25.0.1'
编译'com.google.android.gms:play services auth:15.0.1'
编译“pub.devrel:easypermissions:0.3.0”
编译('com.google.api客户端:谷歌api客户端android:1.23.0'){
排除组:“org.apache.httpcomponents”
}
编译('com.google.api:GoogleAPI服务--'){
排除组:“org.apache.httpcomponents”
}}

在那之后,我可以在我的android应用程序中检索/获取gmail正文的整个步骤是什么

更新:如果有人需要扩展版本(中等):


首先,您需要确保使用Google对您进行身份验证。您可以使用Firebase Auth实现这一点(在使用之前,您必须查看它的配置,有很好的文档可用):

经过身份验证后,您可以使用
FirebaseAuth.getInstance().currentUser

下一步是设置Gmail服务:

            val credential = GoogleAccountCredential.usingOAuth2(
                applicationContext, listOf(GmailScopes.GMAIL_LABELS, GmailScopes.GMAIL_READONLY)
            )
                .setBackOff(ExponentialBackOff())
                .setSelectedAccountName(FirebaseAuth.getInstance().currentUser?.email)

            val service = Gmail.Builder(
                NetHttpTransport(), AndroidJsonFactory.getDefaultInstance(), credential
            )
                .setApplicationName("YourAppName")
                .build()
请注意,以上当然不是生产准备材料

最后,这里是阅读部分:

val messageRead = service.users().messages()?.get(FirebaseAuth.getInstance().currentUser?.email, message?.id)?.setFormat("raw")?.execute()
因此,您可以像下面的
messageRead?.snippet

但有两件不明显的事情需要注意:

  • 您必须期望并处理
    UserRecoverableAuthIOException
    exception。此时,用户必须明确授权您的应用程序对消息执行某些操作

  • 所有这些
    execute
    调用都不是在主线程上处理的


  • 希望有帮助!(对不起,我没有时间写详细的操作方法)。

    请详细说明如何使用java进行操作的完整过程
    val messageRead = service.users().messages()?.get(FirebaseAuth.getInstance().currentUser?.email, message?.id)?.setFormat("raw")?.execute()