在firebase的后台点击通知时启动特殊活动

时间:2017-09-03 15:55:29

标签: android firebase firebase-notifications

我在我的应用中使用了Firebase消息,并通过通知向我的应用发送了一些值,并在点击通知时打开特殊活动。 这适用于forground中的应用程序(应用程序已打开)以及点击打开的通知活动并显示来自firebase的conent。 但我希望在应用关闭时开放活动(背景)。

清单代码:

<activity
            android:name=".activity.NotificationActivity"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="activity_post" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
</activity>

MyFirebaseMessagingService类代码:

@Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Log.d(TAG, "From: " + remoteMessage.getFrom());

        // Check if message contains a data payload.
        if (remoteMessage.getData().size() > 0) {
            Map<String,String> data=remoteMessage.getData();
            switch (data.get(DESTINATION_TYPE)){
                case DESTINATION_TYPE_ACTIVITY:
                    String activity=data.get(DESTINATION);
                    switch (activity){
                      case ACTIVITY_POST:
                            Intent showPostActivityIntent=new Intent(this, NotificationActivity.class);
                            String postTitle=data.get(EXTRA_POST_TITLE);
                            String postContent=data.get(EXTRA_POST_CONTENT);
                            String postImageUrl=data.get(EXTRA_POST_IMAGE_URL);
                            showPostActivityIntent.putExtra("title",postTitle);
                            showPostActivityIntent.putExtra("content",postContent);
                            showPostActivityIntent.putExtra("image_url",postImageUrl);
                            showPostActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            sendNotification(remoteMessage.getNotification().getBody(),showPostActivityIntent);
                    }
                    break;

            }
        }

        // Check if message contains a notification payload.
        if (remoteMessage.getNotification() != null) {
            Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
        }

private void sendNotification(String messageBody,Intent intent) {
        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.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
                .setContentTitle("Tip Top App")
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

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

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

0 个答案:

没有答案