簡體   English   中英

Android通知未顯示在應用程序中

[英]Android notifications not showing up in app

我正在編寫一個使用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());

}

根據評論中的要求,請包括用於處理通知的HomeActivity代碼,以便我們檢查代碼並提出解決方案。 同時,這是我建議您處理HomeActivity類中傳入的GCM通知的HomeActivity (如果有必要,在我看到您的代碼后將更新答案)。 在我的示例中,我有一個輔助方法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);
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM