2016-05-31 130 views
3

我在Android应用程序中实现了Google Cloud Messaging。当我打开应用程序时使用JSON发送消息时,它的行为与关闭应用程序时不同。当应用程序在后台时推送通知

当我有应用程序打开,并收到通知,它开始我希望它启动的意图。当我收到通知时,我的应用程序已关闭,并且我单击通知,它会打开主要目的。我怎么能说我打开应用程序并收到推送通知时需要打开哪个意图?

在MyGcmListenerService代码

public class MyGcmListenerService extends GcmListenerService { 

    private static final String TAG = "MyGcmListenerService"; 
    File fileDir, file; 
    String filename, filestring; 

    /** 
    * Called when message is received. 
    * 
    * @param from SenderID of the sender. 
    * @param data Data bundle containing message data as key/value pairs. 
    *    For Set of keys use data.keySet(). 
    */ 

    @Override 
    public void onMessageReceived(String from, Bundle data) { 

     Bundle notification = data.getBundle("notification"); 
     String title = notification.getString("title"); 
     String naam = notification.getString("naam"); 
     String nfcId = notification.getString("nfcId"); 
     Log.d(TAG, "From: " + from); 
     Log.d(TAG, "Title: " + title); 
     Log.d(TAG, "Naam: " + naam); 
     Log.d(TAG, "nfcId: " + nfcId); 

     if (from.startsWith("/topics/")) { 

     } else { 
      filename = "gegevensOverledene"; 
      ReadWriteFile readWriteFile = new ReadWriteFile(getApplicationContext()); 
      readWriteFile.writeFileOverledene(filename, naam); 
     } 

     sendNotification(title, naam, nfcId); 
    } 

    /** 
    * Create and show a simple notification containing the received GCM message. 
    */ 
    private void sendNotification(String title, String naam, String nfcId) { 
     Intent intent = new Intent(this, PinLoginActivity.class); 
     intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     intent.putExtra("naam", naam); 
     intent.putExtra("nfcId",nfcId); 
     intent.putExtra("Class",NieuweOverledeneActivity.class); 
     PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, PendingIntent.FLAG_ONE_SHOT); 
     Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
     NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
      .setSmallIcon(R.mipmap.ic_launcher) 
      .setContentTitle(title) 
      .setContentText(naam) 
      .setAutoCancel(true) 
      .setSound(defaultSoundUri) 
      .setContentIntent(pendingIntent); 

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

     notificationManager.notify(0 /* ID of notification */, notificationBuilder.build()); 
    } 
} 

我送的JSON,请注意,我离开了token_mobile故意

{ 
    "to": "TOKEN_MOBILE****************", 
    "notification": { 
     "title": "Nieuwe overledene", 
     "body": "Henk", 
     "naam": "Henk", 
     "nfcId": "80-40-A3-4A-C2-D6-04" 
    } 
} 
+0

它是在你的sendNotification方法中,你需要设置正确的意图到PendingIntent你开始 –

+0

无论我在sendNotification中设置了哪个意图,当我在应用程序关闭时收到消息时,它**总是* *启动主要意图 – willemjan92

+1

这很奇怪 - 你可能在你的'PinLoginActivity'中做任何检查,可能是将用户重定向到主要活动?请向我们展示'PinLoginActivity'代码。另外,尝试将您的'意图'标志更改为'intent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);' – ishmaelMakitla

回答

-1

进行以下检查

/** 
* Create and show a simple notification containing the received GCM message. 
*/ 
private void sendNotification(String title, String naam, String nfcId) { 

    //Create the intent according to your condition and pass it to pendingintent. 

    Intent intent = new Intent(this, PinLoginActivity.class); 
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    intent.putExtra("naam", naam); 
    intent.putExtra("nfcId",nfcId); 
    intent.putExtra("Class",NieuweOverledeneActivity.class); 
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, PendingIntent.FLAG_ONE_SHOT); 
    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
     .setSmallIcon(R.mipmap.ic_launcher) 
     .setContentTitle(title) 
     .setContentText(naam) 
     .setAutoCancel(true) 
     .setSound(defaultSoundUri) 
     .setContentIntent(pendingIntent); 

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

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build()); 
} 
+0

当我在应用程序关闭时收到消息时,我在sendNotification中设置了哪个意图并不重要,它**总是**开始主要意图。只有在收到通知时打开应用程序时才会调用sendNotification – willemjan92

3

您正在发送通知消息。详细了解如何处理通知消息here

当您的应用程序处于前台通知消息传递给您的onMessageReceived回调。在那里你可以决定接收到的消息应该做什么,例如:生成并显示通知或与你的应用服务器或其他任何东西同步。

当你的应用是在后台通知消息自动地生成基于在您的下游消息请求的“通知”对象传递的属性通知,onMessageReceived未在此情况下调用。如果你看一下reference docs的通知消息,你会看到一个名为场click_action你可以用这种方法来定义时自动生成的通知被窃听推出什么活动:

在Android上,如果设置,活动当用户单击通知时,将启动匹配的意图过滤器 。

如果未设置,则启动主Activity,这是您现在看到的。

注意:此行为仅适用于通知消息,如果只发送数据消息,则所有发送的消息将导致调用onMessageReceived

+0

如何处理带有应用程序的通知消息在后台? – Sanoop

+0

通知消息由应用程序在后台导致显示的通知时由客户端库处理。如果该通知消息具有伴随的数据有效载荷,则由于用户点击通知而导致启动的附加内容将可用。 –

+0

因此,基本上如果与通知相关的动作应该伴随数据有效载荷或通知有效载荷。我的理解是否正确? – Sanoop

相关问题