Android通知未显示在应用中

时间:2016-05-14 05:31:38

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

我正在编写一个使用GCM推送通知的应用,但是当出现通知时,它会打开应用但不会显示该消息。当我在应用程序中时,消息正常显示。请帮忙。

这是我的清单

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


<uses-permission android:name="android.permission.RECORD_AUDIO" />
<!-- GCM Permissions - Start here -->

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />

<permission
    android:name="com.example.amosang.pushtest.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />

<uses-permission android:name="com.example.amosang.pushtest.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.VIBRATE" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        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:exported="true"
        android:name=".HomeActivity"
        android:label="@string/title_activity_home" >
    </activity>

    <receiver
        android:name=".GCMBroadcastReceiver"
        android:exported="true"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
            <action android:name="com.example.amosang.pushtest" />
        </intent-filter>
    </receiver>

    <service android:name=".GCMNotificationIntentService"
        android:exported="false">

    </service>


    <activity
        android:name=".NewRequest"
        android:label="@string/title_activity_new_request" >
    </activity>
</application>

通知代码

public class GCMNotificationIntentService extends IntentService{


//set ID for the notification, so it can be updated
public static final int notifyID = 9001;
NotificationCompat.Builder builder;
public GCMNotificationIntentService() {
    super("GcmIntentService");
}

@Override
protected void onHandleIntent(Intent intent) {
    Bundle extras = intent.getExtras();
    Log.d("GCMN","GCMNTEST");
    GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);

    String messageType = gcm.getMessageType(intent);

    if (!extras.isEmpty()) {
        if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR
                .equals(messageType)) {
            sendNotification("Send error: " + extras.toString());
        } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED
                .equals(messageType)) {
            sendNotification("Deleted messages on server: "
                    + extras.toString());
        } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE
                .equals(messageType)) {
                sendNotification("Message Received from Google GCM Server:\n\n"
                        + extras.get(AppConstants.MSG_KEY));
        }
    }
    GCMBroadcastReceiver.completeWakefulIntent(intent);
}

private void sendNotification(String msg){

    Intent resultIntent = new Intent(this, HomeActivity.class);
    Log.d("RECEIVEDPT2",msg);
    resultIntent.putExtra("msg", msg);
    PendingIntent resultPendingIntent = PendingIntent.getActivity(this,0,resultIntent,PendingIntent.FLAG_UPDATE_CURRENT);
    NotificationCompat.Builder mNotifyBuilder;
    NotificationManager mNotificationManager;

    mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    mNotifyBuilder = new NotificationCompat.Builder(this).setContentTitle("Alert")
            .setContentTitle("You've received a new message")
            .setSmallIcon(R.drawable.ic_cast_dark);

    //Set pending intent
    mNotifyBuilder.setContentIntent(resultPendingIntent);

    //Set vibration
    int defaults = 0;
    defaults = defaults | Notification.DEFAULT_LIGHTS;
    defaults = defaults | Notification.DEFAULT_VIBRATE;
    defaults = defaults | Notification.DEFAULT_SOUND;

    mNotifyBuilder.setDefaults(defaults);
    // Set the content for Notification
    mNotifyBuilder.setContentText("You have new notifications");
    // Set autocancel
    mNotifyBuilder.setAutoCancel(true);
    // Post a notification
    mNotificationManager.notify(notifyID, mNotifyBuilder.build());

}

1 个答案:

答案 0 :(得分:0)

根据评论中的要求,请包含处理通知的HomeActivity代码,以便我们检查代码并提出解决方案建议。与此同时,我建议您在HomeActivity课程中处理传入的GCM通知(如果需要,我会在看到您的代码时更新答案)。在我的示例中,我有一个辅助方法processIntent(Intent intent)来处理Intent及其Extras。我从onCreateonNewIntent方法中调用此方法。

@Override
    protected void onCreate(Bundle savedInstanceState) {        
        super.onCreate(savedInstanceState);
        setContentView(R.layout.<layout-id>);
        //here I call our processing method
        processIntent(getIntent());
    }

    @Override
    protected void onNewIntent(Intent intent) { 
        super.onNewIntent(intent);
        //call our processing method
        processIntent(getIntent());     
    }

    /**
     * Helper method to process the incoming GCM Notification.
     * @param intent
     */
    private void processIntent(Intent intent){
        if(intent == null) { return; }
        Bundle extras = intent.getExtras();
        try{
            if(extras.containsKey("msg")){
               //possibly a message
               String message = extras.getString("msg");
               Log.i(TAG, "Received Message "+message); 
               //do whatever with the message, like set as Text of TextView, etc
            }
        }
        catch(Exception e){
            Log.e(TAG, "Error Processing GCM Notification Intent", e);
        }
    }