FCM Notification using Android Studio

前端 未结 2 846
渐次进展
渐次进展 2021-01-27 03:26

I managed to send notification message from PHP file and send to mobile phone using Firebase Cloud Messaging (FCM).

Once the user click on the notification message on mo

相关标签:
2条回答
  • 2021-01-27 03:32

    check this.

    private void sendNotification(String messageBody)
    {
     Intent intent = new Intent(this, MainActivity.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.mipmap.ic_launcher)
            .setContentTitle("Firebase Push Notification")
            .setContentText(messageBody)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);
    
       NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    
     notificationManager.notify(0, notificationBuilder.build());
    }
    
    0 讨论(0)
  • 2021-01-27 03:36

    // in service file

    private void sendNotification(String messageBody) {
            Intent intent = new Intent(this, MainActivity.class);
            Log.d("Send","msg" +messageBody);
            Toast.makeText(getApplicationContext(),"msg" +messageBody ,Toast.LENGTH_SHORT );
            intent.putExtra("msg",messageBody);
            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.mipmap.ic_launcher)
                    .setContentTitle("Firebase Push Notification")
                    .setContentText(messageBody)
                    .setAutoCancel(true)
                    .setSound(defaultSoundUri)
                    .setContentIntent(pendingIntent);
    
            NotificationManager notificationManager =
                    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    
            notificationManager.notify(0, notificationBuilder.build());
        }
    

    // in Activity

    Intent intent = getIntent();
                    String text = intent.getStringExtra("msg");
                    Log.d("Received","msg" +text);
                    Toast.makeText(getApplicationContext(),"Received" + text ,Toast.LENGTH_SHORT );
    
    0 讨论(0)
提交回复
热议问题