应用未收到任何Firebase云消息

时间:2018-08-17 16:49:49

标签: android firebase firebase-cloud-messaging

我正在尝试通过Firebase云消息传递打开某些活动。我正在尝试将以下JSON作为通知发送,以打开DAILYDOSE活动。

{
  "to": 
    "/topics/DAILY_DOSE"
  ,
  "data": {
    "extra_information": "This is some extra information"
  },
  "notification": {
    "title": "NEW NOTIFICATION!",
    "text": "Click me to open an Activity!",
    "click_action": "DAILYDOSE"
  }
}

但是我不确定为什么我的应用程序没有收到任何通知。 这是我处理通知的FirebaseMessagingService类:

public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {

    private static final String TAG = "FirebaseMessagingService";

    public FirebaseMessagingService() {}

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        // Check if message contains a data payload.
        if (remoteMessage.getData().size() > 0) {
            Log.d(TAG, "Message data payload: " + remoteMessage.getData());
            try {
                JSONObject data = new JSONObject(remoteMessage.getData());
                String jsonMessage = data.getString("extra_information");
                Log.d(TAG, "onMessageReceived: \n" +
                    "Extra Information: " + jsonMessage);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        // Check if message contains a notification payload.
        if (remoteMessage.getNotification() != null) {
            String title = remoteMessage.getNotification().getTitle(); //get title
            String message = remoteMessage.getNotification().getBody(); //get message
            String click_action = remoteMessage.getNotification().getClickAction(); //get click_action

            Log.d(TAG, "Message Notification Title: " + title);
            Log.d(TAG, "Message Notification Body: " + message);
            Log.d(TAG, "Message Notification click_action: " + click_action);

            sendNotification(title, message, click_action);
        }
    }

    @Override
    public void onDeletedMessages() {

    }

    private void sendNotification(String title, String messageBody, String click_action) {
        Intent intent;
        if (click_action.equals("DAILYDOSE")) {
            intent = new Intent(this, DailyDose.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        } else {
            intent = new Intent(this, ProfileActivity.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.mipmap.ic_launcher)
            .setContentTitle(title)
            .setContentText(messageBody)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

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

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

我在topic活动中定义了ProfileActivity

public class ProfileActivity extends AppCompatActivity {

    private TextView userName, emailAddress, emailVerified;
    private FirebaseAuth mAuth;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile);

        mAuth = FirebaseAuth.getInstance();

        FirebaseMessaging.getInstance().subscribeToTopic("DAILY_DOSE");
    }

}

这是我的清单

<application
   android:allowBackup="true"
   android:icon="@mipmap/ic_launcher"
   android:label="@string/app_name"
   android:roundIcon="@mipmap/ic_launcher_round"
   android:supportsRtl="true"
   android:theme="@style/AppTheme">
   <activity android:name=".LoginActivity">
      <intent-filter>
         <action android:name="android.intent.action.MAIN" />
         <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
   </activity>
   <activity android:name=".SignUpActivity" />
   <activity android:name=".ProfileActivity" />
   <activity android:name=".DailyDose">
      <intent-filter>
         <action android:name="DAILYDOSE"></action>
         <category android:name="android.intent.category.DEFAULT"></category>
      </intent-filter>
   </activity>
   <service
      android:name=".FirebaseMessagingService">
      <intent-filter>
         <action android:name="com.google.firebase.MESSAGING_EVENT"/>
      </intent-filter>
   </service>
</application>

我不确定为什么我没有收到任何通知。

0 个答案:

没有答案