Android:点击推送通知无法打开应用

时间:2015-07-07 03:25:55

标签: android-intent android-activity push-notification google-cloud-messaging

我有一个Android应用程序需要从我的网络服务器到我的Android手机的一些通知。早些时候我曾经收到通知,并且在点击通知时,我的应用程序曾经打开过。但是现在没有任何改变,我收到所有通知,但我不能点击它们再打开我的应用程序。

我已经尝试检查代码和清单的权限,但似乎不知道问题。

以下是一些必需的代码文件:

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.rakibansary.roomautomator">

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

    <permission
        android:name="com.rakibansary.roomautomator.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.rakibansary.roomautomator.permission.C2D_MESSAGE" />


    <application

        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">

        <receiver
            android:name=".receivers.GcmBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND">
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <category android:name="com.rakibansary.roomautomator" />
            </intent-filter>
        </receiver>

        <service android:name=".services.GcmIntentService" />

        <activity
            android:name="com.rakibansary.roomautomator.Splash"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name=".Welcome"
            android:configChanges="locale"/>
        <activity android:name=".CentralBox" />
        <activity android:name=".Register"
            android:configChanges="locale"/>

        <activity
            android:name="com.rakibansary.roomautomator.Home"
            android:label="@string/title_activity_home" />
    </application>

</manifest>

GcmBroadcastReceiver.java

package com.rakibansary.roomautomator.receivers;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.widget.Toast;

import com.rakibansary.roomautomator.CentralBox; // Added from New code

import com.rakibansary.roomautomator.Home;
import com.rakibansary.roomautomator.R;
import com.rakibansary.roomautomator.util.Tools;

import org.json.JSONObject;

/**
 * Created by rakib on 5/10/15.
 */
public class GcmBroadcastReceiver extends BroadcastReceiver {
    private static final String TAG = "GcmBroadcastReceiver";

// Added from new code

    public static final String TYPE_DEVICE_STATUS_MODIFIED = "1";
    public static final String TYPE_BATCH_OPERATION = "2";
    public static final String TYPE_DEVICE_DELETED = "3";
    public static final String BOX_ID = "boxid";

// Add Finish


    @Override
    public void onReceive(Context context, Intent intent) {
        if (Tools.needsLoginOrRegistration(context))
            return;

        context.sendBroadcast(new Intent(Home.FILTER_NOTIFICATION_RECEIVER));

        Bundle extras = intent.getExtras();
        try {
            String type = extras.getString("type");
            JSONObject data = new JSONObject(extras.getString("data"));

            Log.i(TAG, "Push notification received: " + data); // Added from new code

//            if (type.equals("1"))  Commented this and added new If clause below


            if (type.equals(TYPE_DEVICE_STATUS_MODIFIED))   {


                // Added from new code
                final String boxid = data.getString(BOX_ID);
                final String mac = data.getString("mac");
                final boolean powerState = data.getBoolean("ps");
                final boolean startState = data.getBoolean("ss");
                final String temperature = data.getString("temp");
                Intent resultIntent = new Intent(context, Home.class);
                resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                resultIntent.putExtra(Home.EXTRA_CENTRAL_BOX_ID, boxid);
                resultIntent.putExtra(Home.EXTRA_DEVICE_MAC, mac);
                resultIntent.putExtra(Home.EXTRA_POWER_STATE, powerState);
                resultIntent.putExtra(Home.EXTRA_START_STATE, startState);
                resultIntent.putExtra(Home.EXTRA_TEMPERATURE, temperature);
                PendingIntent resultPendingIntent = PendingIntent.getActivity(context, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);

                // Add finished

                NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)

                        .setContentIntent(resultPendingIntent)  // Added from new code
                        .setSmallIcon(R.drawable.ic_launcher)
                        .setContentTitle("Device status modified")
                        .setContentText("Device Mac: " + data.getString("mac"))
                        .setStyle(new NotificationCompat.BigTextStyle().bigText(
                                "Device MAC: " + data.getString("mac") +
                                        "\nPower State: " + (data.getBoolean("ps") ? "On" : "Off") + "\n" +
                                        "Start Sate: " + (data.getBoolean("ss") ? "Running" : "Stopped") + "\n" +
                                        "Temperature: " + data.getString("temp")
                        ));

                NotificationManager mNotifyMgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
                mNotifyMgr.notify(222, mBuilder.build());
            } else if (type.equals(TYPE_BATCH_OPERATION)) {               // Added from new code the if clause
                String boxid = data.getString("boxid");
                Tools.saveCentralBox(context, boxid);

                Intent resultIntent = new Intent(context, Home.class);
                resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
                        Intent.FLAG_ACTIVITY_CLEAR_TASK);

                resultIntent.putExtra(Home.EXTRA_CENTRAL_BOX_ID, boxid);


                PendingIntent resultPendingIntent = PendingIntent.getActivity(
                        context,
                        0,
                        resultIntent,
                        PendingIntent.FLAG_UPDATE_CURRENT
                );

                NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
                        .setSmallIcon(R.drawable.ic_launcher)
                        .setContentTitle("Batch operation performed!")
                        .setContentText("Tap to view details!")
                        .setAutoCancel(true)
                        .setContentIntent(resultPendingIntent);

                NotificationManager notifManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
                notifManager.notify(223, mBuilder.build());
            }   else if (type.equals(TYPE_DEVICE_DELETED)) {                // Added the else if  from new code
                final String boxid = data.getString(BOX_ID);
                final String mac = data.getString("mac");
                Tools.deleteDevice(context, boxid, mac);
                Log.i(TAG, "Device deleted");
                Intent resultIntent = new Intent(context, Home.class);
                resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                resultIntent.putExtra(Home.EXTRA_CENTRAL_BOX_ID, boxid);
                resultIntent.putExtra(Home.EXTRA_DEVICE_MAC, mac);
                resultIntent.putExtra(Home.EXTRA_DEVICE_DELETED, true);
                PendingIntent resultPendingIntent = PendingIntent.getActivity(context, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
                NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
                        .setContentIntent(resultPendingIntent)
                        .setSmallIcon(R.drawable.ic_launcher)
                        .setContentTitle("Device Deleted")
                        .setContentText("Device Mac: " + data.getString("mac"))
                        .setStyle(new NotificationCompat.BigTextStyle().bigText(
                                "Device MAC: " + mac + " was just deleted!"
                        ));
                NotificationManager mNotifyMgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
                mNotifyMgr.notify(224, mBuilder.build());
            }
        } catch (Exception e) {

        }

    }
}

GcmIntentService.java

package com.rakibansary.roomautomator.services;

import android.app.IntentService;
import android.content.Intent;

/**
 * Created by rakib on 5/10/15.
 */
public class GcmIntentService extends IntentService {

    public GcmIntentService() {
        super("GcmIntentService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {

    }
}

1 个答案:

答案 0 :(得分:1)

您可以在通知中添加Intent以实现此目的。

GcmBroadcastReceiver.javaNotificationManager mNotifyMgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);之间的mNotifyMgr.notify(224, mBuilder.build());

您应该添加以下代码:

Notification notification = mBuilder.build();
Intent notificationIntent = new Intent(context, MainActivity.class);

notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                | Intent.FLAG_ACTIVITY_SINGLE_TOP);

PendingIntent intent = PendingIntent.getActivity(context, 0,
                notificationIntent, 0);

notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;