2

Push Notificationが受信された後、Notificationが押されたときにÀctivityを開きます。ここでAndroid CGM開始プッシュ通知を受信した後のアクティビティ

は私Services次のとおりです。

public class GCMIntentService extends GcmListenerService { 
@Override 
public void onMessageReceived(String from, Bundle data) { 
    String title = data.getString("title"); 
    String message = data.getString("message"); 

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) 
      .setSmallIcon(R.mipmap.ic_launcher) 
      .setContentTitle(title) 
      .setContentText(message) 
      .setStyle(new NotificationCompat.BigTextStyle().bigText(message)) 
      .setTicker(message) 
      .setAutoCancel(true); 

    mBuilder.setDefaults(Notification.DEFAULT_LIGHTS); 
    mBuilder.setDefaults(Notification.DEFAULT_SOUND); 
    mBuilder.setDefaults(Notification.DEFAULT_VIBRATE); 


    Intent myIntent = new Intent(this, MyActivity.class); 
    PendingIntent intent2 = PendingIntent.getBroadcast(this, 1, myIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
    mBuilder.setContentIntent(intent2); 

    notificationManager.notify(1, mBuilder.build()); 
} 



public class GCMIDListenerService extends InstanceIDListenerService { 
@Override 
public void onTokenRefresh() { 
    InstanceID instanceID = InstanceID.getInstance(this); 
    String token; 
    try { 
     token = instanceID.getToken(Resources.getSystem().getString(R.string.gcm_defaultSenderId), GoogleCloudMessaging.INSTANCE_ID_SCOPE, null); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    //TODO:send token to the server 
    } 
} 

私はちょうどGoogleドキュメント以下のすべてをしました。私はいつか別のプロジェクトのためにその機能を実装しましたが、それはちょっと違って、Googleが提供する 'com.google.android.gms.gcm.GcmReceiver'の代わりに私自身の 'BroadcastReceiver'を使いました。ここで

は私Manifestです:

<application> 

... 

<!-- GCM Push Notifications Config --> 
    <receiver 
     android:name="com.google.android.gms.gcm.GcmReceiver" 
     android:exported="true" 
     android:permission="com.google.android.c2dm.permission.SEND" > 
     <intent-filter> 
      <action android:name="com.google.android.c2dm.intent.RECEIVE" /> 
      <category android:name="gcm" /> 
     </intent-filter> 
    </receiver> 
    <service 
     android:name=".push.GCMIntentService" 
     android:exported="false" > 
     <intent-filter> 
      <action android:name="com.google.android.c2dm.intent.RECEIVE" /> 
     </intent-filter> 
    </service> 
    <service 
     android:name=".push.GCMIDListenerService" 
     android:exported="false"> 
     <intent-filter> 
      <action android:name="com.google.android.gms.iid.InstanceID" /> 
     </intent-filter> 
    </service> 


</application> 

任意のアイデア?何か不足していますか?

はここにいくつかのドキュメント私がフォローを持っている:

CGM tutorial By Dustin Rothwell

Google official documentation

そしてもちろん、私は stackoverflowで、ここで多くのことを研究しています。

ありがとうございます!

答えて

2

あなたは少しミスをしました。あなたはアクティビティを開くためにこのコードを書いています。

PendingIntent intent2 = PendingIntent.getBroadcast(this, 1, myIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
mBuilder.setContentIntent(intent2); 

アクティビティを開きたいとあなたはgetActivity方法の代わりに、getBroadcast方法を使用して保留中の意図を作成する必要があります。

希望すると、これが役立ちます。

+0

恐ろしく!どうもありがとう :) – Ale

関連する問題