2017-03-01 47 views
-1

我試圖讓我的通知上比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,Str PendingIntent)

錯誤:執行任務':app:compileDebugJavaWithJavac'失敗。

Compilation failed; see the compiler error output for details.

我的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

你的問題到底是什麼,api級別? – Remario

+0

你的錯誤日誌在哪裏? [mcve] –

回答

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()); 
+0

它使用支持庫。 – Remario

+0

我已經更新了我的問題,請參見上面 –

+0

你嘗試了COMPAT庫我寫 – Remario