GCM:广播意图回调:result = CANCELLED forIntent {act = com.google.android.c2dm.intent.RECEIVE

时间:2019-07-11 10:10:20

标签: android push-notification firebase-cloud-messaging google-cloud-messaging

我正在模拟器和某些设备上收到通知,但是一旦该应用被后台杀死,我在许多手机上都没有收到通知。 我已经尝试了互联网上的所有答案,但没有一个对我有用。 任何答案将不胜感激。

这是我完成的代码。

我的项目的清单文件                                                         

    <service android:name="esports.gamerji.com.notification_services.MyFirebaseInstanceIDService"
        tools:ignore="InnerclassSeparator"
        android:exported="false">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
        </intent-filter>
    </service>

    <meta-data
        android:name="com.google.firebase.messaging.default_notification_icon"
        android:resource="@drawable/notification" />

    <meta-data
        android:name="com.google.firebase.messaging.default_notification_color"
        android:resource="@color/colorAccent" />

这是我的firebaseMessaging服务代码。

  public class MyFirebaseMessagingService extends FirebaseMessagingService

{

private static final String TAG = "MyFirebaseMsgService";
public static int badge_count = 0;
private static final String NOTIFICATION_CHANNEL_ID = "10001";
private NotificationCompat.Builder mBuilder;
private NotificationManager mNotificationManager;
Random random = new Random();

/**
 * Called when message is received.
 *
 * @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
 */
// [START receive_message]
@Override
public void onMessageReceived(RemoteMessage remoteMessage)
{
    if (remoteMessage.getData() == null)
    {
        if (remoteMessage.getNotification() != null)
        {
            if (remoteMessage.getNotification().getBody() != null)
            {
                sendNotification(remoteMessage.getNotification().getBody(), "",
                        remoteMessage.getNotification().getTitle());
            }
        }
        return;
    }

    if (remoteMessage.getData().size() > 0)
    {
        Log.e(TAG, "Message data payload: " + remoteMessage.getData());
    }
    Intent intent = new Intent("NotificationReceived");
    try
    {
        sendNotification(remoteMessage.getNotification().getBody(),"",
                remoteMessage.getNotification().getTitle());
        /*if (!remoteMessage.getData().get("league_id").equals("") || !remoteMessage.getData().get("league_id").equals(null))
        {
            sendNotification(remoteMessage.getNotification().getBody(), remoteMessage.getData().get("league_id"),
                    remoteMessage.getNotification().getTitle());

            sendBroadcast(intent);

            if (!remoteMessage.getData().get("league_id").equals("") || !remoteMessage.getData().get("league_id").equals(null))
            {
                sendNotification(remoteMessage.getNotification().getBody(), remoteMessage.getData().get("league_id"),
                        remoteMessage.getNotification().getTitle());
                sendBroadcast(intent);
            }
        }*/
    }
    catch (NullPointerException e)
    {
        e.printStackTrace();
        if (remoteMessage.getNotification() != null)
        {
            if (remoteMessage.getNotification().getBody() != null)
            {
                sendNotification(remoteMessage.getNotification().getBody(), "",
                        remoteMessage.getNotification().getTitle());
            }
        }
    }


}

private void sendNotification(String messageBody, String league_id, String title)
{
    Intent resultIntent;
    if (Pref.getValue(getApplicationContext(), Constants.IS_LOGIN, false, Constants.FILENAME))
    {
        resultIntent = new Intent(getApplicationContext(), SplashActivity.class);
    }
    else
    {
        resultIntent = new Intent(getApplicationContext(), ActivityMain.class);
    }
    if (!league_id.isEmpty())
        resultIntent.putExtra("mLeagueId", league_id);
    resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent resultPendingIntent = PendingIntent.getActivity(getApplicationContext(),
            0  /*Request code*/, resultIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder = new NotificationCompat.Builder(getApplicationContext());
    mBuilder.setSmallIcon(R.drawable.notification);
    mBuilder.setContentTitle(title)
            .setContentText(messageBody)
            .setAutoCancel(true)
            .setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
            .setContentIntent(resultPendingIntent);

    mNotificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
    {
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", importance);
        notificationChannel.enableLights(true);
        notificationChannel.setLightColor(getColor(R.color.colorPrimaryDark));
        notificationChannel.enableVibration(true);
        notificationChannel.setVibrationPattern(new long[]{100, 100, 100});
        assert mNotificationManager != null;
        mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
        mBuilder.setAutoCancel(true);
        mNotificationManager.createNotificationChannel(notificationChannel);
    }
    assert mNotificationManager != null;
    mNotificationManager.notify(random.nextInt(9999 - 1000) + 1000 /*Request Code*/, mBuilder.build());
}

}

0 个答案:

没有答案