我的環境配置:Windows 10、Unity 5.6.0f3、Java JDK 1.8.0_121、Android Studio 2.2.3。
 
 
在推播中加入震動:
 
Notification.Builder builder = new Notification.Builder(context.getApplicationContext());
builder.setVibrate(new long[] {1000, 500, 1000, 400, 1000, 300, 1000, 200, 1000, 100});
 
 
這邊針對 long 陣列做說明
陣列中的第 1 個值為 "多久後震動",第 2 個值為 "震動多久",這是第 1 組震動設定。
陣列中的第 3 個值為 "多久後震動",第 4 個值為 "震動多久",這是第 2 組震動設定。
兩個值為一個組合,依此類推。
 
時間單位為毫秒,1000 毫秒 = 1 秒鐘。
 
所以 new long[] {1000, 500, 1000, 400, 1000, 300, 1000, 200, 1000, 100} 意思如下
推播啟動後等待 1 秒鐘,1 秒鐘後震動 0.5 秒。
接著再繼續等待 1 秒鐘,1 秒鐘後震動 0.4 秒。
接著再繼續等待 1 秒鐘,1 秒鐘後震動 0.3 秒。
接著再繼續等待 1 秒鐘,1 秒鐘後震動 0.2 秒。
接著再繼續等待 1 秒鐘,1 秒鐘後震動 0.1 秒。
 
 
在推播中加入閃光:
 
Notification.Builder builder = new Notification.Builder(context.getApplicationContext());
builder.setLights(Color.RED, 1000, 500);
 
 
這邊針對閃光做說明
網路上的查到的解釋是說,參數有 argb、onMs、offMs。
argb 的意思為要亮什麼顏色。
onMs 的意思為要亮多久。
offMs 的意思為要暗多久。
也就是說 setLights(Color.RED, 1000, 500) 的意思為,亮紅燈持續 1 秒鐘後,熄滅 0.5 秒鐘,接著再亮 1 秒鐘後熄滅 0.5 秒鐘,如此重複。
 
但問題來了,這邊有些手機是不支援的,然後......
我不知道是不是我的手機不支援,至少這段程式碼我不管怎麼調整,手機就是沒反應......
 
再麻煩有成功的朋友們,可以在下方留言實際的功用,讓我有講解錯誤也可以馬上修正,感謝!
 
 
在推播中加入音效:
 
Notification.Builder builder = new Notification.Builder(context.getApplicationContext());

// 系統預設音效
builder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);

// 使用自己的音效
int sound = context.getResources().getIdentifier("doorbell", "raw", context.getPackageName());
Uri soundUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + context.getPackageName() + "/" + sound);
builder.setSound(soundUri);
 
 
這邊針對音效做說明
基本上使用系統預設音效,沒有什麼多大的問題。
而使用自己準備的音效時,你就必須注意到,檔案名稱必須全小寫,並且要放在 res/raw 目錄底下。
因為我是要當成 Plugin 放在 Unity 上使用的,所以做法上有點不同。
首先我必須先取得 "doorbell" 音效檔的資源 ID,接著取得檔案的路徑後再設定給推播程式,讓程式要播放音效時可以載入得到音效檔資源。
 
PS:音效的音量我用 ASUS ZenFone 3 測試的結果,主要是 "鈴聲與通知音音量" 在控制的,而非 "媒體音量" 與 "鬧鐘音量"。
image09.jpg
 
 
以下是使用之前的範例,再添加上以上功能的步驟講解:
 
步驟一、
PS:我目前的 Unity、Android Studio 版本皆於範例版本不同,不過下載直接執行,一樣是可運行的。
 
 
步驟二、
使用 Android Studio 開啟範例的 Android 專案
image01.jpg
 
 
步驟三、
這邊一樣,目錄顯示方式更換為 "Project",並且開啟 app/src/main/java/com.test.tw.test/LocalNotificationReceiver 這支檔案
image02.jpg
 
 
步驟四、
將上面的震動、閃光、音效程式碼添加進
 
package com.test.tw.test;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;

import com.unity3d.player.UnityPlayerActivity;


public class LocalNotificationReceiver extends BroadcastReceiver {

    public LocalNotificationReceiver() {
    }

    @Override
    public void onReceive( Context context, Intent intent ) {

        String title = intent.getStringExtra("title");
        String content = intent.getStringExtra("content");
        String iconSmall = intent.getStringExtra("iconSmall");
        int requestCode = intent.getIntExtra("requestCode", 0);

        // 這邊表示點擊推播訊息後, 要返回 Unity, 所以必須是 UnityPlayerActivity.class
        Intent newIntent = new Intent(context, UnityPlayerActivity.class);
        newIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

        PendingIntent pendingIntent = PendingIntent.getActivity(context, requestCode, newIntent, PendingIntent.FLAG_CANCEL_CURRENT); // 取得PendingIntent

        // 取得 icon 圖示
        int smallIcon = context.getResources().getIdentifier(iconSmall, "drawable", context.getPackageName());

        // 取得音效資源
        int sound = context.getResources().getIdentifier("doorbell", "raw", context.getPackageName());
        Uri soundUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + context.getPackageName() + "/" + sound);

        // 建立推播內容
        Notification.Builder builder = new Notification.Builder(context.getApplicationContext());

        builder.setSmallIcon(smallIcon);    // setSmallIcon 尺寸建議 32 * 32
        builder.setContentTitle(title);
        builder.setContentText(content);
        builder.setContentIntent(pendingIntent);
        builder.setAutoCancel(true);

        // 第一個值為多久後震動, 第二個值為震動多久
        builder.setVibrate(new long[] {1000, 500, 1000, 400, 1000, 300, 1000, 200, 1000, 100});

        // 設定閃燈效果, 參數依序為顏色、亮的時間與暗的時間, 單位是毫秒
        builder.setLights(Color.RED, 1000, 500);

        // 系統預設音效
        // builder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);

        // 使用自己的音效
        builder.setSound(soundUri);
        Notification notification = builder.build();


        // 取得推播管理器, 執行推播
        NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(requestCode, notification);
    }

}
 
 
 
步驟五、
匯出 JAR 檔案
image03.jpg
 
 
步驟六、
開啟範例的 Unity 專案,將剛匯出的 JAR 檔案覆蓋掉 Unity 內的 JAR。
Android Studio 範例匯出的預設路徑為 Android Studio/Test/app/release。
而範例 Unity 專案的 JAR 檔案位置在 Assets/Plugins/Android 底下。
image04.jpg
 
 
步驟七、
在 Assets/Plugins/Android/res 底下,按下滑鼠右鍵,建立一個資料夾,取名為 "raw",這名稱跟位置是系統規定的
image05.jpg
 
把音效檔丟進去
image06.jpg
 
 
步驟八、
接上手機後,打包並執行
image07.jpg
 
APK 位置隨便放就可以了,這邊我放在專案內
image08.jpg
 
 
最後開啟 APP,按下本地推播的按鈕,並且等待 5 秒鐘,就可以看到推播出現囉!並且會有自訂音效與震動功能!
 
 
arrow
arrow

    岳 發表在 痞客邦 留言(0) 人氣()