在FCM PushNotification中的后台应用时未调用onMessageReceived方法

时间:2016-10-19 10:33:32

标签: android firebase-cloud-messaging

我遇到了这个问题:当我的应用程序处于后台并点击通知时,它应该启动我想要的活动(ChatActivity),但这不会发生。

我不知道如何使用getIntent.getExtra(),我无法找到解决此问题的正确方法。

我想在点击通知而不是SplashScreen启动时用一些参数启动有问题的ChatActivity。当我的应用程序处于前台时,ChatActivity应该启动。

这是我的代码:

public class FcmMessagingService extends FirebaseMessagingService
{
    private static final String TAG = "FcmMessagingService";

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage)
    {

        sendNotification(remoteMessage.getData().get("title"),remoteMessage.getData().get("body"));

        Log.d(TAG, "From: " + remoteMessage.getFrom());
        // Check if message contains a data payload.
        if (remoteMessage.getData().size() > 0)
        {
            Log.d(TAG, "Message data payload: " + remoteMessage.getData());
            System.out.println("Message data payload: " + remoteMessage.getData());

        }

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

    }
    private void sendNotification(String messageTitle,String messageBody) {

        Intent intent = new Intent(this, ChatActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this,0 /* request code */, intent,PendingIntent.FLAG_UPDATE_CURRENT| PendingIntent.FLAG_ONE_SHOT);

        long[] pattern = {500,500,500,500,500};

        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.app_icon)
                .setContentTitle(messageTitle)
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setVibrate(pattern)
                .setLights(Color.BLUE,1,1)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());


}

AndroidManifest: -

  <application
        android:allowBackup="true"
        android:icon="@drawable/app_icon"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/MyMaterialTheme">
        <activity
            android:name=".SplashActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Home.InboxActivity"
            android:screenOrientation="portrait"
            >
        </activity>
        <activity
            android:name=".Home.HomeActivity"
            android:screenOrientation="portrait"
            android:windowSoftInputMode="stateHidden"
            >
        </activity>
        <activity
            android:name=".Home.HistoryActivity"
            android:screenOrientation="portrait"
            >
        </activity>
        <activity
            android:name=".Home.DetailMail"
            android:screenOrientation="portrait"
            >
        </activity>
        <activity
            android:name=".Home.ChatActivity"
            android:screenOrientation="portrait"
            android:windowSoftInputMode="stateHidden"
            >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

        <service
            android:name=".PushNotification.FcmMessagingService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT"/>
            </intent-filter>
        </service>

        <service
            android:name=".PushNotification.FcmInstanceIdService">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
            </intent-filter>
        </service>



    </application>

我的数据回复

data payload: {tag=, body=sdf, badge=90, image=xyz.png, sound=default, title=XYZ}

日志

10-19 18:01:49.455 25107-25174/? W/Babel: bcp TOOK TOO LONG! (15025ms > 10000ms)
10-19 18:01:49.561 25107-25187/? W/Babel: bcp TOOK TOO LONG! (15032ms > 10000ms)

                                          --------- beginning of system
10-19 18:01:49.663 750-1957/? W/Telecom: TelecomServiceImpl: null is not visible for the calling user
10-19 18:01:49.670 750-1623/? W/Telecom: TelecomServiceImpl: ComponentInfo{com.google.android.talk/com.google.android.apps.hangouts.telephony.TeleConnectionService}, [e44cca915afc0390d8a77c0ff4608152603e7fa3], UserHandle{0} is not visible for the calling user
10-19 18:01:49.673 25107-25107/? I/Babel_telephony: TeleModule.updateConnectionManagerRegistration, registration preference changed from false to false
10-19 18:01:49.674 25107-25107/? W/Babel: BAM#gBA: invalid account id: -1
10-19 18:01:49.674 25107-25107/? W/Babel: BAM#gBA: invalid account id: -1
10-19 18:01:49.674 25107-25107/? I/Babel_telephony: TeleModule.updateIncomingCallRegistration, preferred account for incoming calls changed from: null to null
10-19 18:01:49.676 750-1965/? W/Telecom: TelecomServiceImpl: null is not visible for the calling user
10-19 18:01:49.678 750-1689/? W/Telecom: TelecomServiceImpl: ComponentInfo{com.google.android.talk/com.google.android.apps.hangouts.telephony.TeleConnectionService}, [e44cca915afc0390d8a77c0ff4608152603e7fa3], UserHandle{0} is not visible for the calling user
10-19 18:01:49.851 25107-25199/? W/Babel: bcp TOOK TOO LONG! (15036ms > 10000ms)
10-19 18:01:49.957 1217-25451/? E/MediaPlayer-JNI: QCMediaPlayer mediaplayer NOT present
10-19 18:01:49.996 313-25453/? D/ExtendedUtils: Try creating ExtendedExtractor
10-19 18:01:49.999 313-25453/? D/ExtendedUtils: Discarding default extractor and using the extended one
10-19 18:01:50.004 313-25453/? I/FFmpegExtractor: android-source:0xaab8c268
10-19 18:01:50.013 313-25453/? I/FFMPEG: Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'android-source:0xaab8c268':
10-19 18:01:50.013 313-25453/? I/FFMPEG:   Metadata:
10-19 18:01:50.013 313-25453/? I/FFMPEG:     major_brand     : M4A 
10-19 18:01:50.013 313-25453/? I/FFMPEG:     minor_version   : 0
10-19 18:01:50.013 313-25453/? I/FFMPEG:     compatible_brands: M4A mp42isom
10-19 18:01:50.013 313-25453/? I/FFMPEG:     creation_time   : 2009-08-12 22:37:16
10-19 18:01:50.013 313-25453/? I/FFMPEG:     title           : Facebook Pop
10-19 18:01:50.013 313-25453/? I/FFMPEG:     artist          : Facebook
10-19 18:01:50.013 313-25453/? I/FFMPEG:     compilation     : 0
10-19 18:01:50.013 313-25453/? I/FFMPEG:     gapless_playback: 0
10-19 18:01:50.013 313-25453/? I/FFMPEG:     encoder         : iTunes 8.2.1, QuickTime 7.6.2
10-19 18:01:50.013 313-25453/? I/FFMPEG:     iTunSMPB        :  00000000 00000840 000000E1 00000000000016DF 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
10-19 18:01:50.013 313-25453/? I/FFMPEG:     Encoding Params : vers
10-19 18:01:50.013 313-25453/? I/FFMPEG:     iTunNORM        :  000001AE 00000000 000001AE 00000000 00000000 00000000 00007F16 00000000 00000000 00000000
10-19 18:01:50.013 313-25453/? I/FFMPEG:   Duration: 00:00:00.19, start: 0.000000, bitrate: 191 kb/s
10-19 18:01:50.014 313-25453/? I/FFMPEG:     Stream #0:0(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, mono, fltp, 24 kb/s (default)
10-19 18:01:50.014 313-25453/? I/FFMPEG:     Metadata:
10-19 18:01:50.014 313-25453/? I/FFMPEG:       creation_time   : 2009-08-12 22:37:16
10-19 18:01:50.014 313-25453/? D/FFmpegExtractor: FFmpegExtrator, url: android-source:0xaab8c268, format_name: mov,mp4,m4a,3gp,3g2,mj2, format_long_name: QuickTime / MOV
10-19 18:01:50.014 313-25453/? I/FFmpegExtractor: adjust mime(video/mp4 -> audio/mp4a-latm)
10-19 18:01:50.014 313-25453/? D/FFmpegExtractor: suppoted codec(aac) by official Stagefright
10-19 18:01:50.014 313-25453/? D/FFmpegExtractor: ffmpeg detected media content as 'audio/mp4a-latm' with confidence 0.08
10-19 18:01:50.014 313-25453/? D/ExtendedUtils: Try creating ExtendedExtractor
10-19 18:01:50.023 313-25453/? D/ExtendedUtils: Discarding default extractor and using the extended one
10-19 18:01:50.024 1217-25451/? E/MediaPlayer: Should have subtitle controller already set
10-19 18:01:50.027 750-1623/? I/MediaFocusControl:  AudioFocus  requestAudioFocus() from android.media.AudioManager@22fd031a req=3flags=0x0
10-19 18:01:50.029 313-16421/? D/NuPlayerDriver: start(0xaab5fe68)
10-19 18:01:50.029 313-25452/? I/GenericSource: start

请帮我解决这个问题?

4 个答案:

答案 0 :(得分:1)

我遇到同样的问题就是诀窍。将消息保存在我的案例中我使用了sharedpref你可以使用Const来保存消息或通过意图传递Etra。快乐编码:&gt;)

userbeer

在Firebase控制台上,这是我指定的关键信息和标题

Firebase.Console see here

beer

}

答案 1 :(得分:1)

我得到了答案..当您通过FCM在数据有效负载中获得json响应时,system.out.println(getIntent.getExtra())您从系统托盘中获得了值,我们可以通过此管理我们的活动。

这里是Launcher ACtivity的代码

 bundle = getIntent().getExtras();
                if(bundle!=null) {
        for (String key : bundle.keySet()) {
            Object value = bundle.get(key);
            Log.d("DATA_SENT", String.format("%s %s (%s)", key, value.toString(), value.getClass().getName()));
            System.out.println("messa vale" + String.format("%s %s (%s)", key, value.toString(), value.getClass().getName()));

            if (key.equals("changeit_id")) {
                valu = value.toString();
                System.out.println("messa Value found:" + value);
            }
        }
    }
        if(!valu.equalsIgnoreCase(""))
        {
            Intent intent = new Intent(this, ChatActivity.class);
            intent.putExtra("item_id", valu);
            startActivity(intent);
            finish();
        }
        else
        {
            new Handler().postDelayed(new Runnable()
            {
                @Override
                public void run()
                {
                    Intent i = new Intent(SplashActivity.this, HomeActivity.class);
                    finish();
                    startActivity(i);
                }
            }, 3000);
        }

答案 2 :(得分:0)

根据Firebase handling messages,有两种类型的消息:数据和通知。如果您的应用处于后台并且您发送了通知消息,则不会调用onMessageReceived。这些是未调用onMessageReceived时的两个例外:

  

当您的应用在后台时发送通知。在此   如果是,通知将传送到设备的系统托盘。一个   用户点按通知会默认打开应用启动器。

     

包含通知和数据有效负载的消息,包括背景和   前景。在这种情况下,通知将传递给   设备的系统托盘,数据有效负载在附加功能中提供   您的启动器活动的意图。

如果remoteMessage.getData()remoteMessage. getNotification()都不为空且应用处于后台状态,则会在启动器活动的附加内容中接收数据部分。从那里你必须明确地打电话给你的聊天活动。

enter image description here

答案 3 :(得分:0)

在admin.php中添加click_action

$fields = array(

        'to'                        => $reg_id ,
        'priority'                  => "high",
        'notification'              => array( "title" => "Android Learning", "body" => $message, "tag" => $tag, "click_action" => "OPEN_ACTIVITY_1" ),
        'data'                      => array("message" =>$message, "image"=> $img_url),
    );

并将click_action设置为OPEN_ACTIVITY_1以触发如下所示的意图过滤器:

<activity android:name=".HomeActivity">
    <intent-filter>
        <action android:name="OPEN_ACTIVITY_1" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>