简体   繁体   English

无法在android中设置推送通知声音

[英]unable to set push notification sound in android

I have created the push notification application, I'm getting the message from GCM services but push notification sound is not working. 我已经创建了推送通知应用程序,我从GCM服务获取消息,但推送通知声音不起作用。 I need to play sound when I'm getting notification.I have posted my code below anyone help me with this... 当我收到通知时,我需要播放声音。我已将我的代码发布到任何人的帮助下我...

//receiver class//


import android.app.Activity;
import android.app.NotificationManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.support.v4.content.WakefulBroadcastReceiver;
import android.widget.Toast;

public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {

  @Override
  public void onReceive(Context context, Intent intent) 
  {
    ComponentName comp = new ComponentName(context.getPackageName(),GCMNotificationIntentService.class.getName());

    startWakefulService(context, (intent.setComponent(comp)));

    setResultCode(Activity.RESULT_OK);

    generateNotification(context);

    System.out.println("received push notification");

    Toast.makeText(context,"received notification",Toast.LENGTH_LONG).show();
  }

private void generateNotification(Context context)
{
    // TODO Auto-generated method stub
    /*NotificationManager mBuilder=(NotificationManager)context.getSystemService(context.NOTIFICATION_SERVICE);

    Uri notificationsound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    mBuilder.setSound(notificationsound);
    */
}

protected void onHandleIntent(Intent intent) {
    // TODO Auto-generated method stub

}
}




package com.everest.jewellerapp;

import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import com.google.android.gms.gcm.GoogleCloudMessaging;

public class GCMNotificationIntentService extends IntentService
{


  public static final int NOTIFICATION_ID = 1;

  private NotificationManager mNotificationManager;

  NotificationCompat.Builder builder;

  public GCMNotificationIntentService() 
  {
    super("GcmIntentService");
  }

  public static final String TAG = "GCMNotificationIntentService";

  @Override
  protected void onHandleIntent(Intent intent) 
  {
    Bundle extras = intent.getExtras();

    GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);

    String messageType = gcm.getMessageType(intent);

    if (!extras.isEmpty()) 
    {
      if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) 
      {
        sendNotification("Send error: " + extras.toString());
      }
      else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) 
      {
        sendNotification("Deleted messages on server: "
            + extras.toString());
      } 
      else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) 
      {

        for (int i = 0; i < 3; i++) {
          Log.i(TAG,
              "Working... " + (i + 1) + "/5 @ "
                  + SystemClock.elapsedRealtime());
          try 
          {
            Thread.sleep(5000);
          } 
          catch (InterruptedException e)
          {
          }

        }
        Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());

        sendNotification("Message Received from Google GCM Server: "+ extras.get(Config.MESSAGE_KEY));



        Log.i(TAG, "Received: " + extras.toString());
      }
    }
    GcmBroadcastReceiver.completeWakefulIntent(intent);
  }

  private void sendNotification(String msg) 
  {
    Log.d(TAG, "Preparing to send notification...: " + msg);

    mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,new Intent(this, MainActivity.class), 0);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.ic_launcher)
        .setContentTitle("ijeweller")
        .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
        .setContentText(msg);

    mBuilder.setContentIntent(contentIntent).setContentTitle("").setContentText("");
    mBuilder.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 }); 
    mBuilder.setLights(Color.RED, 3000, 3000); 


    Uri notificationsound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    mBuilder.setSound(notificationsound);


    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

    Log.d(TAG, "Notification sent successfully.");
  }
}

permissions 权限

<uses-permission android:name="android.permission.INTERNET" />

<!-- GCM requires a Google account. -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />

<!-- Keeps the processor from sleeping when a message is received. -->
<uses-permission android:name="android.permission.WAKE_LOCK" />

<!-- Creates a custom permission so only this app can receive its messages. -->
<permission
    android:name="com.everest.jewellerapp.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />

<uses-permission android:name="com.everest.jewellerapp.permission.C2D_MESSAGE" />

<!-- This app has permission to register and receive data message. -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

<!-- Network State Permissions to detect Internet status -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<!-- Permission to vibrate -->
<uses-permission android:name="android.permission.VIBRATE" />

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" /> 
      <uses-permission android:name="android.permission.CALL_PHONE"/>
      <uses-permission android:name="android.permission.RECEIVE_MMS" />
      <uses-permission android:name="android.permission.RECEIVE_SMS" />
      <uses-permission android:name="android.permission.READ_SMS" />

I use the default notification sound. 我使用默认通知声音。 Maybe this will work: 也许这会奏效:

mBuilder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);
mBuilder.setDefaults(Notification.DEFAULT_SOUND)

This worked for me, though the setDefaults() method does overwrite any vibration or LED light settings, so if you are wanting those as well you might have to set the sound directly. 这对我setDefaults() ,虽然setDefaults()方法确实会覆盖任何振动或LED灯设置,所以如果你也想要那些,你可能需要直接设置声音。 If default lights and/or vibration is fine you can bitwise OR the other static constants in Notification 如果默认灯光和/或振动很好,您可以在Notification按位或其他静态常量

I use this code: 我用这个代码:

Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
r.play();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM