我无法通过通知打开指定的活动

时间:2016-12-06 10:37:36

标签: android firebase android-manifest firebase-notifications

我使用Firebase通知在我的Android应用中发送通知。我想实现以下目标: 当用户点击通知时,我的选择活动应该打开,活动应该自动执行一些我自己定义的操作,比如在webview中加载特定网址等。

我做了什么?

在Firebase控制台中,在自定义数据下,我将key设置为click_action。价值为ttdemo并发送通知。

在我的清单中:

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".aboutmain" />
    <activity android:name=".Timetable">
    <intent-filter>
        <action android:name="ttdemo"/>
    </intent-filter>

    </activity>
    <activity android:name=".Results" />
    <activity android:name=".timetable_downloads" />
    <activity android:name=".result_downloads" />
    <activity android:name=".eforms" />
    <activity android:name=".write" />
    <activity android:name=".qpapers" />
    <activity android:name=".november2014" />
    <activity android:name=".november_2014_downloads" />
    <activity android:name=".april_2015" />
    <activity android:name=".november_2015" />
    <activity android:name=".april_2016"></activity>
</application>

当前情况: 我收到通知但没有打开时间表活动。 相反,主要活动会打开。

3 个答案:

答案 0 :(得分:0)

在调用onMessageReceived方法时尝试使用吹码。

public void onMessageReceived(RemoteMessage remoteMessage) {
    Map<String, String> data = remoteMessage.getData();
    if (data.containsKey("click_action")) {
        String className = data.get("click_action");
        Class cls;
        try {
            cls = Class.forName(className);
            Intent i = new Intent(context, cls);
            //i.putExtras(extras); // put extras if you required
            context.startActivity(i);
        }catch(ClassNotFoundException e){
            //means you made a wrong input in firebase console
        }           
    }
}

要使用firebase控制台发送通知,请将键值对作为自定义数据放置,如下所示:

Key: click_action
Value: <fully qualified class name of your activity>

答案 1 :(得分:0)

您需要将panding intent传递给FirebaseMessagingService类的onMessageReceived(RemoteMessage remoteMessage)方法中的通知

 Intent resultIntent = new Intent(FirebaseMessagingService.this, MobilActivity.class);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent intent = PendingIntent.getActivity(getApplicationContext(), 0,
                    resultIntent, PendingIntent.FLAG_CANCEL_CURRENT);

并将通知对象设置为

Notification notification;
        notification = mBuilder.setSmallIcon(icon).setTicker(title).setWhen(0)
.setContentIntent(resultPendingIntent)

之类的方法
private void showSmallNotification(NotificationCompat.Builder mBuilder, int icon, String title, String message, String timeStamp, PendingIntent resultPendingIntent, Uri alarmSound) {
    NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
    inboxStyle.addLine(message);
    Notification notification;
    notification = mBuilder.setSmallIcon(icon).setTicker(title).setWhen(0)
            .setAutoCancel(true)
            .setContentTitle(title)
            .setContentIntent(resultPendingIntent)
            .setSound(alarmSound)
            .setStyle(inboxStyle)
            .setWhen(System.currentTimeMillis())
            .setSmallIcon(R.drawable.growth_indus)
            .setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), icon))
            .setContentText(message)
            .build();
    NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notificationManager.notify(AppConstants.getParamInt(AppConstants.GlobalParamEnum.NOTIFICATION_ID), notification);
}

答案 2 :(得分:0)

最后,我实现了目标!

此解决方案对我来说非常合适! http://androidbash.com/firebase-push-notification-android/