Android - GCM push notifications not appearing in notifications list

前端 未结 2 1706
慢半拍i
慢半拍i 2021-01-12 17:26

I\'m working on my first Android app to use the Google Cloud Messaging (GCM) service for push notifications. I\'ve got to the point where I can successfully send a message

2条回答
  •  温柔的废话
    2021-01-12 17:52

    If you are using GcmListenerService you can use this code, add to your onMessageReceived the sendNotification()

    @Override
    public void onMessageReceived(String from, Bundle data) {
            String message = data.getString("message");
            sendNotification(message);
    }
    
    private void sendNotification(String message) {
            Intent intent = new Intent(this, YOURCLASS.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
            PendingIntent.FLAG_ONE_SHOT);
    
            Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.ic_park_notification)
                    .setContentTitle("Ppillo Message")
                    .setContentText(message)
                    .setAutoCancel(true)
                    .setSound(defaultSoundUri)
                    .setContentIntent(pendingIntent);
    
            NotificationManager notificationManager =
                    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    
            notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
        }
    

提交回复
热议问题