Android :Tap on Push Notification does not open Ap

2019-06-05 15:06发布

问题:

I have an android app which requires some notifications from my web-server to my android phone . Earlier I used to receive notifications and on tapping the notification , my application used to open . However without anything being changed now , I receive all notifications but I cannot tap on them to open my application anymore .

I have tried checking the code and the manifest for permissions , but dosen't seem to know the problem .

Here are some of the required code files :

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:

You may add a Intent to your notification to make it happen.

In your GcmBroadcastReceiver.java between NotificationManager mNotifyMgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); and mNotifyMgr.notify(224, mBuilder.build());

You should add following code:

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;