簡體   English   中英

Android-如何為GCM消息設置自定義通知聲音

[英]Android-How to set Custom notification sound for GCM messages

我想為GCM通知設置自定義提醒聲音。這是我當前的編碼。

Uri notification = RingtoneManager
                .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        Ringtone r = RingtoneManager.getRingtone(
                context.getApplicationContext(), notification);
        if(r!= null){
         r.play();
        }
Intent i = new Intent(context.getApplicationContext(),
                CjStartingActivity.class);

我在Res文件夾(Res.raw.mi.mp3)中有mp3文件。

使用以下代碼:

private void sendNotification(String msg) {
    mNotificationManager = (NotificationManager) this
            .getSystemService(Context.NOTIFICATION_SERVICE);

    Intent intent = new Intent(this, LoginActivity.class);
    intent.putExtra(IAppConstants.IS_NOTIFICATION, true);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            intent, 0);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
            this).setSmallIcon(R.drawable.notification_icon)
            .setContentTitle("App Name")
            .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
            .setContentText(msg).setAutoCancel(true);

    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
    Uri uri =Uri.parse("android.resource://" + getPackageName() + "/"+ R.raw.sound);
     Ringtone ring = RingtoneManager.getRingtone(getApplicationContext(), uri);
     ring.play();
    // for vibrating phone
    ((Vibrator) getApplicationContext().getSystemService(
            Context.VIBRATOR_SERVICE)).vibrate(800);

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM