PendingIntent not working with FCM notification

时间:2016-07-11 20:10:51

标签: android android-intent android-pendingintent notificationmanager

i want to open Detail activity on notification click event. Here is the PendingIntent and Notification set class. I added addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) also added PendingIntent.FLAG_UPDATE_CURRENT. I searched all topics on site i found this solutions i tried all of them.

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    private static final String TAG = "MyFirebaseMsgService";
    /**
     * 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) {
        // TODO(developer): Handle FCM messages here.
        // If the application is in the foreground handle both data and notification messages here.
        // Also if you intend on generating your own notifications as a result of a received FCM
        // message, here is where that should be initiated. See sendNotification method below.
        Log.d(TAG, "From: " + remoteMessage.getFrom());
        Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());

        ArrayList<String> notificationData = new ArrayList<String >(remoteMessage.getData().values());
        String fortuneID = notificationData.get(0);
        sendNotification(remoteMessage.getNotification().getBody(),fortuneID);
    }
    // [END receive_message]

    /**
     * Create and show a simple notification containing the received FCM message.
     *
     * @param messageBody FCM message body received.
     */
    private void sendNotification(String messageBody,String notificationID) {

        Intent configureIntent = new Intent(getApplicationContext(), FalDetayActivity.class);
        configureIntent.putExtra("extra", "123123");
        configureIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        configureIntent.setAction("dummy_unique_action_identifyer" + "123123");
        int dummyuniqueInt = new Random().nextInt(543254);
        PendingIntent pendingClearScreenIntent = PendingIntent.getBroadcast(getApplicationContext(), dummyuniqueInt, configureIntent, PendingIntent.FLAG_UPDATE_CURRENT);

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

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("myApp")
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingClearScreenIntent);

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

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

Here is the getBundle part MainActivity

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

        String quote = getIntent().getStringExtra("extra");

        ...
}

Another problem i want to open FalDetailActivity but always open MainActiviy and getIntent() always return null. Probably, my PendingIntent not set i did not found error please check this code. Thanks...

UPDATE

I update getBroadcast to getActivity and remove something

        Intent configureIntent = new Intent(getApplicationContext(), FalDetayActivity.class);
        configureIntent.putExtra("extra", "123123");
        configureIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingClearScreenIntent = PendingIntent.getActivity(getApplicationContext(), 0, configureIntent, PendingIntent.FLAG_UPDATE_CURRENT);

2 个答案:

答案 0 :(得分:1)

最后我找到了解决方案: https://firebase.google.com/docs/notifications/android/console-audience

当您的应用在后台时,Android会将通知消息定向到系统托盘。用户点按通知会默认打开应用启动器。

这包括同时包含通知和数据有效负载的消息。在这些情况下,通知将传递到设备的系统托盘,并且数据有效负载将在启动器活动的附加内容中传递。

答案 1 :(得分:0)

您希望启动具有待定意图的活动,而不是发送广播。 将function nextFrame ( frameArray, whichFrame ) { if( whichFrame >= frameArray.length ) { return } if( typeof frameArray[ whichFrame ].startFunc === 'function' ) { frameArray[ whichFrame ].startFunc.call(frameArray[ whichFrame ].el) }; frameArray[ whichFrame ].el.animate( frameArray[ whichFrame ].animation, frameArray[ whichFrame ].dur, frameArray[ whichFrame ].easing, nextFrame.bind(null, frameArray, whichFrame + 1 ) ); } // Example of use var r = s.rect(100,100,100,100,20,20).attr({ stroke: '#123456', 'strokeWidth': 20, fill: 'red' }); var c = s.circle(50,50,50).attr({ stroke: '#123456', 'strokeWidth': 20, fill: 'blue' }); var g = s.group(r,c); var myFrames = [ { el: g, animation: { transform: 'r360,150,150' }, dur: 1000, easing: mina.bounce }, { el: r, animation: { transform: 't100,-100s2,3', fill: 'green' }, dur: 1000, easing: mina.bounce }, { el: r, animation: { transform: 't100,100' }, dur: 1000, easing: mina.bounce, startFunc: sayHello }, { el: g, animation: { transform: 's2,1' }, dur: 1000, easing: mina.bounce }, { el: r, animation: { transform: 's1,2' }, dur: 1000, easing: mina.bounce }, { el: c, animation: { transform: 's1,1' }, dur: 1000, easing: mina.bounce }]; nextFrame( myFrames, 0 ); function sayHello() { alert('hello, this is me ==> ' + this); } 更改为PendingIntent.getBroadcast(...)

不要使用PendingIntent.getActivity(...),而是尝试使用服务的上下文,即将getApplicationContext()替换为getApplicationContext()

由于您是从服务的上下文启动活动,因此您还需要添加新的任务意图标志 -

this

此外,请将这些标志用于待处理的意图 -

configureIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);