6

Google Cloud Messaging(GCM)サービスをプッシュ通知に使用するための最初のAndroidアプリを開発中です。サーバーアプリケーションからメッセージを正常に送信し、クライアントアプリケーションのGCMIntentServiceクラス内のonMessageイベントにメッセージの内容を記録することができます。しかし、私は、メッセージが受信されたことをデバイス上で視覚的に表示していません。 iPhoneの場合と同様に、プルダウン通知リストにメッセージが表示されることを期待していました。これは手動でコード化する必要がありますか?また、どのアクティビティが現在アクティブであるかに関わらず、またそのアプリがバックグラウンドでアイドル状態であるかどうかにかかわらず、メッセージを表示する一般的な方法がありますか?どんな助けもありがたい。Android - GCMプッシュ通知が通知リストに表示されない

答えて

7

このコードは、画面上部のアンドロイドシステムバーに通知を生成します。このコードは、トップバーの通知をクリックした後、ユーザーを "Home.class"に誘導する新しいインテントを作成します。現在のアクティビティに基づいて特定の処理を実行したい場合は、GCMIntentServiceから他のアクティビティへのブロードキャスト要求を送信できます。

Intent notificationIntent=new Intent(context, Home.class); 
generateNotification(context, message, notificationIntent); 

private static void generateNotification(Context context, String message, Intent notificationIntent) { 
    int icon = R.drawable.icon; 
    long when = System.currentTimeMillis(); 
    NotificationManager notificationManager = (NotificationManager) 
      context.getSystemService(Context.NOTIFICATION_SERVICE); 
    Notification notification = new Notification(icon, message, when); 
    String title = context.getString(R.string.app_name); 

    // set intent so it does not start a new activity 
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | 
      Intent.FLAG_ACTIVITY_SINGLE_TOP); 
    PendingIntent intent =PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
    notification.setLatestEventInfo(context, title, message, intent); 
    notification.flags |= Notification.FLAG_AUTO_CANCEL; 
    notificationManager.notify(0, notification); 
} 

この例が動作するように存在することが必要になりますが、それはあなたのアイデアを与える必要がありますR.drawableとR.String内のリソースを使用することに注意してください。ステータス通知の詳細については、http://developer.android.com/guide/topics/ui/notifiers/index.htmlとこれを参照してください。あなたがGcmListenerServiceを使用している場合http://developer.android.com/reference/android/content/BroadcastReceiver.html

+0

に追加し、このコードを使用することができます。 –

+0

こんにちは、私はこのプラグインhttps://github.com/marknutter/GCM-Cordovaを使用し、すべての指示に従っています。しかし、私はアプリ内でのみメッセージを受け取ることができますが、アンドロイド通知エリアに通知を表示するようにアプリを取得できません。お知らせ下さい。私はこのプラグインを使用しています。私はphonegapを使用しています。@Zachary Moshansky –

+0

申し訳ありませんが、私はPhonegapを一度も使用していません。通知の仕方についてコメントできません。 –

1

あなたは素晴らしい、多くのおかげですあなたのonMessageReceived sendNotificationを()

@Override 
public void onMessageReceived(String from, Bundle data) { 
     String message = data.getString("message"); 
     sendNotification(message); 
} 

private void sendNotification(String message) { 
     Intent intent = new Intent(this, YOURCLASS.class); 
     intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 
     PendingIntent.FLAG_ONE_SHOT); 

     Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
     NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
       .setSmallIcon(R.drawable.ic_park_notification) 
       .setContentTitle("Ppillo Message") 
       .setContentText(message) 
       .setAutoCancel(true) 
       .setSound(defaultSoundUri) 
       .setContentIntent(pendingIntent); 

     NotificationManager notificationManager = 
       (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

     notificationManager.notify(0 /* ID of notification */, notificationBuilder.build()); 
    } 
関連する問題