2017-08-31 57 views
3

我一直在重寫我的代碼以匹配Android O通知更改,並提醒通知停止工作。我的目標是SDK版本26,下面是我的代碼。Android O正式發佈通知

請幫我領導通知工作。任何幫助表示讚賞。

public void createNotificationChannel() { 
NotificationChannel messagesChannel = new NotificationChannel(MESSAGES_CHANNEL, context.getString(R.string.channel_messages), 
      NotificationManagerCompat.IMPORTANCE_HIGH); 
    messagesChannel.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION), 
      new AudioAttributes.Builder().setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION).build()); 
    messagesChannel.setVibrationPattern(new long[]{0, 1000, 1000, 1000, 1000}); 
    messagesChannel.setLightColor(Color.WHITE); 
    messagesChannel.setShowBadge(true); 
    notificationManager.createNotificationChannel(messagesChannel); 
} 
    public void showMessageNotification(final String conversationId, final String message) { 
     Intent contentIntent = ConversationDetailsActivity.getLaunchIntent(this, conversationId); 
     contentIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

     TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); 
     stackBuilder.addParentStack(ConversationDetailsActivity.class); 
     stackBuilder.addNextIntent(contentIntent); 

     PendingIntent contentPendingIntent = stackBuilder.getPendingIntent(conversationId.hashCode(), FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT); 

     Notification notification = buildNotification(R.drawable.ic_message_24dp, message, contentPendingIntent); 
     notificationManager.notify(conversationId.hashCode(), notification); 
    } 
    private Notification buildNotification(final int iconResId, final String message, final PendingIntent contentPendingIntent) { 
     NotificationCompat.Builder builder = new NotificationCompat.Builder(this, MESSAGES_CHANNEL); 
     Notification notification = builder 
       .setSmallIcon(iconResId) 
       .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)) 
       .setContentTitle(getString(R.string.notification_title)) 
       .setContentText(message) 
       .setContentIntent(contentPendingIntent) 
       .setPriority(NotificationCompat.PRIORITY_HIGH) 
       .build(); 
     notification.flags |= Notification.FLAG_AUTO_CANCEL; 
     return notification; 
    } 

從我的build.gradle

compileSdkVersion 26 
buildToolsVersion '26.0.1' 
defaultConfig { 
    applicationId "com.xxxxxxxx" 
    minSdkVersion 17 
    targetSdkVersion 26 
    versionCode 21 
    versionName '1.13' 
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 
    multiDexEnabled true 
} 
+0

你編譯sdk 26嗎?我可以看到你的gradle.build(app) – MrAppMachine

+0

@MrAppMachine增加了build.gradle –

回答

1

原來我忘了爲我的通知設置聲音/振動。只有在設置了聲音/振動的優先級> =高的通知中才能使用。

0

確保您創建了一個通知管理器。您可以瞭解如何設置通知here

希望這有助於!

+0

NotificationManager不是你創建的東西。我得到它的實例是這樣的:notificationManager =(NotificationManager)context.getSystemService(NOTIFICATION_SERVICE);並使用它。 –

+0

是否嘗試過我發佈的鏈接中的代碼 – MrAppMachine

+1

這正是我從中獲取代碼的地方。它不起作用 –