Notification最佳實踐

jopen 8年前發布 | 6K 次閱讀 Android開發 移動開發 Notification

長按通知以后進入自定義設置頁面

<activity android:name="SettingsActivity"
        android:label="@string/app_name">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name=
            "android.intent.category.DEFAULT" />
        <category android:anme=
            "android.intent.category.NOTIFICATION_PREFERENCES" />
    </intent-filter>
</activity>

作者順便推廣了一把通過 PreferenceFragment 與 Preference xml 很簡單就實現一個設置頁面.

Add more actionable button on Notification

new NotificationCompat.Builder(this)
    .setSamllIcon(...)
    .setContentTitle(title)
    .setContentText(text)
    .addAction(R.drawable.ic_answer,
        getString(R.string.call_answer),
        answerPendingIntent)
    .addAction(R.drawable.ic_ignore,
        getString(R.string.call_ignore),
        ignorePendingIntent)
    .build();

new Notification.Builder(this)
    .setSmallIcon(...)
    .setContentTitle(title)
    .setContentText(text)
    // started in Jelly Bean
    .setStyle(
        new Notification.BigPictureStyle()
        .bigPicture(photoBitmap))
    .build();

    // rich text?
    .setStyle(
        new Notification.BigTextStyle()
            .bigText(longText))
    .build();

多端同步,如果一端的notification被干掉,同步到其他端也將其干掉。

onGoing Notification

用戶無法劃掉

  • startForeground()
  • incoming calls
  • prefer snooze-and-repost pattern

需要給用戶可以劃掉/干掉的機會

如:音樂播放器在播放的時候無法劃掉,在暫停/停止的時候,允許用戶劃掉(改優先級)(Google Music目前的做法)如: SSH/V*N連接在連接上的時候無法劃掉,但是提供Action斷開鏈接就自動干掉

IV. Delight the user

兩種用戶:

  1. 從來都不開聲音,要不只開振動
  2. 始終都開通知聲音,并且喜歡去設置聲音類型針對不同的應用

設置通知聲音

<RingtonePreference
    android:persistent="true"
    android:key="sms_sound"
    android:denpendency="sms_enable"
    android:ringtoneType="notification"
    android:title="@string/sms_sound" />
SharedPreferences prefs = PreferenceManager
    .getDefaultSharedPreferences(context);
String url = prefs.getString(SOUND, null);
if(uri != null){
    builder.setSound(Uri.parse(uri));
}

V. Connect them to the people they love

系統提供指定聯系人(星標) ,其余聯系人不打擾的模式,因此我們可以在不請求獲取聯系人權限的情況下通過以下的方式進行對通知綁定

new Notification.Builder(this)
    .setSmallIcon(...)
    .setLargeIcon(...)
    .setContentTilte(senderName)
    .setContentText(msgText)
    .addAction(...)
    // email
    .addPerson(Uri.fromParts("mailto",
        "igzhenjie@gmail.com", null)
        .toString())
    .build();

    // tel
    .addPerson(Uri.fromParts("tel",
        "1(617) 555-1212", null)
        .toString())

來自: http://blog.dreamtobe.cn/2016/01/09/notification_best_practise/

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!