更改我的通知代码

时间:2017-03-01 19:45:19

标签: android android-notifications

我试图让我的通知工作在比API22更高的api上,

我努力让它发挥作用,

如果我能告诉别人或告诉我需要改变哪些内容会很棒。

我变得如此接近,但每次都失败了。

以下是我的代码:

public class Utils {
    public static final int NOTIFY_ID1 = 1001;

    public static void notifyMessage(Context context, String msg, Activity activity){
        //Notification builder; 
        PendingIntent contentIntent = null;
        NotificationManager nm;
        // The NotificationManager object is required to send the notification
        nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
        // Message object

        Intent notificationIntent = new Intent(context, activity.getClass());
        // PendingIntent.getActivity(Context context, int requestCode, Intent intent, int flags)
        // Used to get a pending PendingIntent,Let the Intent start a new Activity to handle the notification
        contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);


        //Defines the content information that appears in the notification bar

        int icon = R.drawable.chat;
        long when = System.currentTimeMillis();
        Notification notification = new Notification(icon, msg, when);
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        notification.defaults |= Notification.DEFAULT_SOUND; // Call the system with sound

        notification.flags |= Notification.FLAG_AUTO_CANCEL; // Clicking the Clear button or clicking on the notification will automatically disappear

        notification.defaults |= Notification.DEFAULT_LIGHTS;
        notification.vibrate = new long[]{300, 500};
        notification.setLatestEventInfo(context, "BluetoothChat", msg, contentIntent);


       /* // Customize the notification style we want to display in the status bar

        builder = new Notification(context);
        builder.setContentIntent(contentIntent)
            .setSmallIcon(R.drawable.ic_launcher)//Set the icon in the status bar(Small icon)
            .setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.i5))//Drop the drop-down list inside the icon(Big icon)        
            .setTicker("this is bitch!") //Set the status bar to display the information
            .setWhen(System.currentTimeMillis())//Set the time to occur
            .setAutoCancel(true)//The settings can be cleared
            .setContentTitle("This is ContentTitle")//Set the title in the drop-down list
            .setContentText("this is ContentText");//Set the context content

        */
        // Get the notification object you just created

        // Notification notification = builder.getNotification();//Get a Notification


        // send a notification message via NotificationManger
         // Parameter 1 notification ID, parameter 2 which notification to send

        nm.notify(NOTIFY_ID1, notification);
    }
}

当我添加:



notification.setLatestEventInfo(context, "BluetoothChat", msg, contentIntent);




我收到以下错误:

错误:(43,21)错误:找不到符号方法setLatestEventInfo(Context,String,String,PendingIntent)

错误:任务':app:compileDebugJavaWithJavac'执行失败。

  

编译失败;有关详细信息,请参阅编译器错误输出。

我的构建gradle:



android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId 'app.name'
        minSdkVersion 19
        targetSdkVersion 25
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
    productFlavors {
    }
}

dependencies {
    compile 'com.android.support:appcompat-v7:25.1.1'
    compile 'com.android.support:support-v4:25.1.1'
    compile 'com.android.support:cardview-v7:25.1.1'
    compile files('libs/commons-io-2.4.jar')
}




1 个答案:

答案 0 :(得分:0)

您可以尝试NotificationCompat。实施例。

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
                    .setContent(remoteNotification)
                    .setVisibility(NotificationCompat.VISIBILITY_PUBLIC).
                    setPriority(NotificationCompat.PRIORITY_HIGH).setSmallIcon(R.mipmap.ic_logo)
                    .setCustomHeadsUpContentView(remoteNotification)
                    .setStyle(new android.support.v7.app.NotificationCompat.DecoratedCustomViewStyle());