无法解析符号setLatestEventInfo

时间:2016-06-26 05:46:26

标签: java android notifications

我在Android Studio中实现了一个PendingIntent示例..但NoticationClass似乎有点问题..

public void onClick(View v) {

                NotificationManager notif=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
                Notification notify=new Notification(R.drawable.noti,tittle,System.currentTimeMillis());

                notify.setLatestEventInfo(getApplicationContext(),subject,body,pending);

            }

这是我的Gradle

apply plugin: 'com.android.application'

android {

    compileSdkVersion 23
    buildToolsVersion "23.0.1"

    defaultConfig {
        applicationId "testproject.example.com.testproject"
        minSdkVersion 22
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.2.0'
    compile 'com.android.support:design:23.2.0'
    compile 'com.google.android.gms:play-services:6.5.87'

}

我有一个使用NotificationBuilder的建议,但那是NotificationCompat类的替代,我的代码中没有这个。

我见过这个系统的多个解决方案,但无法实现任何一个

setLatestEventInfo的其他替代方法。

任何建议都会有所帮助

1 个答案:

答案 0 :(得分:1)

setLatestEventInfo已在Api 23中删除。

有关详细信息,请访问:https://developer.android.com/sdk/api_diff/23/changes/android.app.Notification.html

要支持Api 23 and upper,您必须按以下方式实施。

声明此全局

int currentapiVersion = android.os.Build.VERSION.SDK_INT;

然后像这样检查

if (currentapiVersion > LOLLIPOP_MR1) {

            notification = new Notification(icon, text, time);
            notification.setLatestEventInfo(this, title, text, contentIntent); // This method is removed from the Android 6.0
            notification.flags |= Notification.FLAG_AUTO_CANCEL;
            mNM.notify(NOTIFICATION, notification);
        } else {
            NotificationCompat.Builder builder = new NotificationCompat.Builder(
                    this);
            notification = builder.setContentIntent(contentIntent)
                    .setSmallIcon(icon).setTicker(text).setWhen(time)
                    .setAutoCancel(true).setContentTitle(title)
                    .setContentText(text).build();

            mNM.notify(NOTIFICATION, notification);
        }

在其他方面,你必须实现唯一的NotificationCompat.Builder

UpDate:

这样的工作示例。

Intent intent = new Intent(ctx, HomeActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder b = new NotificationCompat.Builder(ctx);

    b.setAutoCancel(true)
     .setDefaults(Notification.DEFAULT_ALL)
     .setWhen(System.currentTimeMillis())         
     .setSmallIcon(R.drawable.ic_launcher)
     .setTicker("Hearty365")            
     .setContentTitle("Default notification")
     .setContentText("Lorem ipsum dolor sit amet, consectetur adipiscing elit.")
     .setDefaults(Notification.DEFAULT_LIGHTS| Notification.DEFAULT_SOUND)
     .setContentIntent(contentIntent)
     .setContentInfo("Info");


    NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(1, b.build());
相关问题