Android- Firebase推送通知单击事件

时间:2018-09-13 07:29:50

标签: android firebase firebase-cloud-messaging

我遵循了该线程Adobe site来处理click_action,当应用关闭时,它可以完美地工作。但是当打开应用程序并且如果我推送通知时,应用程序崩溃并显示以下错误:

Theme: themes:{default=overlay:com.cyngn.hexo, iconPack:com.cyngn.hexo, fontPkg:com.cyngn.hexo, com.android.systemui=overlay:com.cyngn.hexo, com.android.systemui.navbar=overlay:com.cyngn.hexo}
                                                                               java.lang.NullPointerException: Attempt to invoke virtual method 'void android.os.Bundle.unparcel()' on a null object reference
                                                                                   at android.os.Bundle.putAll(Bundle.java:182)
                                                                                   at android.content.Intent.putExtras(Intent.java:7098)
                                                                                   at com.seven.sample.fcm.ClickActionHelper.startActivity(ClickActionHelper.java:20)
                                                                                   at com.seven.sample.fcm.MyFirebaseMessagingService.onMessageReceived(MyFirebaseMessagingService.java:33)
                                                                                   at com.google.firebase.messaging.FirebaseMessagingService.handleIntent(Unknown Source)
                                                                                   at com.google.firebase.iid.zzc.run(Unknown Source)
                                                                                   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
                                                                                   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
                                                                                   at java.lang.Thread.run(Thread.java:818)

下面是我的ClickActionHelper类和FirebaseMessaging服务代码

ClickActionHelper.java

public class ClickActionHelper {

public static void startActivity(String className, Bundle extras, Context context){
    Class cls;

    try {
            cls = Class.forName(className);
            Intent i = new Intent(context, cls);
            i.putExtras(extras);
            context.startActivity(i);


    }catch(ClassNotFoundException e){
        Log.e("NotificationError",e.getMessage());
        //means you made a wrong input in firebase console
    }

 }
}      

MyFirebaseMessagingService

public class MyFirebaseMessagingService extends FirebaseMessagingService {

private static final String TAG = "MyGcmListenerService";

@Override
public void onMessageReceived(RemoteMessage message) {

    Map<String, String> data = message.getData();
    if (data.containsKey("click_action")) {
        ClickActionHelper.startActivity(data.get("click_action"), null, this);
    }

    String image = message.getNotification().getIcon();
    String title = message.getNotification().getTitle();
    String text = message.getNotification().getBody();
    String sound = message.getNotification().getSound();

    int id = 0;
    Object obj = message.getData().get("id");
    if (obj != null) {
        id = Integer.valueOf(obj.toString());
    }

    this.sendNotification(new NotificationData(image, id, title, text, sound));
}


/**
 * Create and show a simple notification containing the received GCM message.
 *
 * @param notificationData GCM message received.
 */
private void sendNotification(NotificationData notificationData) {

    Intent intent = new Intent(this, LandingPage.class);
    intent.putExtra(notificationData.getTitle(), notificationData.getTextMessage());

    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    NotificationCompat.Builder notificationBuilder = null;
    try {

        notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("Sample")
                .setContentText(notificationData.getTextMessage()+"")
                .setAutoCancel(true)
                .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                .setContentIntent(pendingIntent);

    } catch (Exception e) {
        e.printStackTrace();
    }

    if (notificationBuilder != null) {
        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(notificationData.getId(), notificationBuilder.build());
    } else {
        Log.d(TAG, "NotificationBuilder");
    }
}

}

0 个答案:

没有答案