FCM通知,当应用程序处于前台而非后台时,通知单击以打开url / playstore

时间:2019-01-02 06:33:11

标签: android firebase android-studio android-intent firebase-cloud-messaging

在我的代码下方

public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {
    private static final String TAG = "MyFirebaseMsgService";
    public static final String CHANNEL_ID_1="channel_1";
    NotificationManager manager;
    String urlOfAds;

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        Log.d(TAG, "FROM:" + remoteMessage.getFrom());

        //Check if the message contains data
        if(remoteMessage.getData().size() > 0) {
            Log.d(TAG, "Message data: " + remoteMessage.getData());
            Log.d(TAG, "Url: "+remoteMessage.getData().get("app_url"));
            Log.d(TAG, "All data: "+remoteMessage.getData().toString()+" ");
            urlOfAds=remoteMessage.getData().get("app_url");
        }




        //Check if the message contains notification

        if(remoteMessage.getNotification() != null) {
            Log.d(TAG, "Mesage body:" + remoteMessage.getNotification().getBody());
            sendNotification(remoteMessage.getNotification().getTitle(),remoteMessage.getNotification().getBody(),urlOfAds);
           // openStore(getApplicationContext(),remoteMessage);
        }
    }






// Generating Push Notification
    private void sendNotification(String title, String messageBody,String urlOfAds) {
        Intent notificationIntent = new Intent(Intent.ACTION_VIEW);
        notificationIntent.setData(Uri.parse("http://www.google.com"));
        PendingIntent pi = PendingIntent.getActivity(FirebaseMessagingService.this, 0, notificationIntent, 0);
        // Resources r = getResources();
        Notification notification = new NotificationCompat.Builder(FirebaseMessagingService.this)
                .setTicker("yortext")
                .setSmallIcon(android.R.drawable.ic_menu_report_image)
                .setContentTitle(title)
                .setContentText(messageBody)
                .setContentIntent(pi)
                .setAutoCancel(true)
                .build();

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

        notificationManager.notify(0, notification);
    }

    }

0 个答案:

没有答案