首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >FCM PushNotification中后台应用程序时未调用的PushNotification方法

FCM PushNotification中后台应用程序时未调用的PushNotification方法
EN

Stack Overflow用户
提问于 2016-10-19 10:33:32
回答 4查看 1.9K关注 0票数 2

我被这个问题困住了:当我的应用程序处于后台,我点击一个通知,它应该启动我想要的活动(ChatActivity),但这并没有发生。

我不知道如何使用getIntent.getExtra(),也找不到正确的解决方案。

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

这是我的密码:

代码语言:javascript
复制
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());


}

仙女座宣言:-

代码语言:javascript
复制
  <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}

日志

代码语言:javascript
复制
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

请你帮我解决这个问题好吗?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2016-10-20 05:22:59

我知道答案了..。当您在Data中得到FCM的json响应时,system.out.println(getIntent.getExtra())从系统托盘中得到值,我们可以通过它来管理我们的活动。

这里是发射器ACtivity的代码

代码语言:javascript
复制
 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);
        }
票数 1
EN

Stack Overflow用户

发布于 2016-10-19 15:03:32

我也有过同样的问题,这就是诀窍。保存消息,在我的情况下,我使用sharedpref可以使用Const保存消息或通过Etra on意图传递消息。(快乐编码:>)

代码语言:javascript
复制
public class SplashScreen extends Acivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    //When Notification is tapped
    if (getIntent().getExtras() != null) {
        //init message
        String message = String.valueOf(getIntent().getExtras().get("message"));
        String title = String.valueOf(getIntent().getExtras().get("title"));
        //save the message
        MySharedPreference.save(getApplicationContext() , "message" , message);
        MySharedPreference.save(getApplicationContext() , "title" , title);

        startActivity(new Intent(getApplicationContext() , ChatActivity.class));
        finish();
    }

在Firebase控制台上,这是我指定的键消息和标题的位置。

Firebase.Console见这里

代码语言:javascript
复制
public final class MySharedPreference {

public static void save(Context context ,String key , String value){
    SharedPreferences prefs= context.getSharedPreferences(YOUR_PACKAGE_NAME, Context.MODE_PRIVATE);
    SharedPreferences.Editor editor= prefs.edit();
    editor.putString(key,value);
    editor.apply();

}

public static String getValue(Context context ,String key){
    //if 1st time , register the user
    SharedPreferences prefs = context.getSharedPreferences(YOUR_PACKAGE_NAME, Context.MODE_PRIVATE);
    return prefs.getString(key , "");
}

}

票数 1
EN

Stack Overflow用户

发布于 2016-10-19 10:41:30

根据火源处理消息,有两种类型的消息:数据和通知。如果您的应用程序处于后台,并且发送通知消息,则不会调用onMessageReceived。当不调用onMessageReceived时,这是两个例外:

通知是在应用程序处于后台时传递的。在本例中,将通知传递到设备的系统托盘。默认情况下,用户点击通知就会打开应用程序启动程序。 具有通知和数据有效负载的消息,包括后台和前台。在本例中,通知被传递到设备的系统托盘,数据有效负载在启动程序活动的目的之外传递。

如果remoteMessage.getData()remoteMessage. getNotification()都不是null,并且app处于后台状态,那么数据部分将在启动程序活动的附加部分中接收。从此,您将不得不显式地调用您的聊天活动。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40128881

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档