android GCM推,没有振动,没有声音,没有抬头

时间:2017-01-17 02:55:48

标签: android

我正在使用GCM发出通知。

我要求应用服务器将推送通知发送给用户,服务器执行此操作。

当收到推送通知时,该消息很好地显示在屏幕上。 但它没有任何声音或振动,当然也没有抬头。

所以,这是我的代码 当然,我已经添加了两个使用权限(WAKE_LOCK,VIBRATE)

public class FirebaseMessagingService  extends com.google.firebase.messaging.FirebaseMessagingService {
    private static final String TAG = "FirebaseMsgService";

    // [START receive_message]
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

       // sendPushNotification(remoteMessage.getData().get("message"));



        if(remoteMessage.getData().size() > 0){

            sendPushNotification(remoteMessage.getNotification().getBody());

        }

    }

    private void sendPushNotification(String message) {
        System.out.println("received message : " + message);

        Intent intent = new Intent(this, Activity_Login_Main.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0  //Request code
                , intent,
                PendingIntent.FLAG_ONE_SHOT);

        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);  
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.backicon).setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher) )
                .setContentTitle("Push Title ")
                .setContentText(message)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setDefaults(Notification.DEFAULT_SOUND)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        PowerManager pm = (PowerManager) this.getSystemService(Context.POWER_SERVICE);
        PowerManager.WakeLock wakelock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG");
        wakelock.acquire(5000);

        notificationManager.notify(0 // ID of notification
    , notificationBuilder.build());
    }
}

1 个答案:

答案 0 :(得分:0)

在实施Firebase通知时,我遇到了类似的情况。 问题是当您从没有数据部分的firebase控制台发送推送通知时,它将作为通知消息接收。 您应该尝试发送数据消息以触发onMessageReceived()

详情请见:https://firebase.google.com/docs/cloud-messaging/concept-options