【问题标题】:android GCM push, no vibration, no sound, no head upandroid GCM推送,无震动,无声音,无抬头
【发布时间】:2017-01-17 02:55:48
【问题描述】:

我正在使用 GCM 发出通知。

我要求应用服务器向用户发送推送通知,服务器会这样做。

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

所以,这是我的代码 当然,我已经添加了两个 uses-permission(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());
    }
}

【问题讨论】:

    标签: android


    【解决方案1】:

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

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

    【讨论】:

    • 我用 Log 检查了这两种方法(onMessageReceived、sendPushNotification)是否有效。我发现它运作良好......
    猜你喜欢
    • 2021-02-10
    • 2020-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-24
    • 2013-03-18
    • 1970-01-01
    相关资源
    最近更新 更多