2016-12-04 142 views
1

此代码在应用程序处于前台运行状态时运行,并针对任何新消息在建筑物通知中运行。但是我想在My Application在后台时实现新聊天消息的通知。所以根据我的说法,当我使用'服务'在后台运行它时会发生,但我很困惑要实现它。任何想法或建议如何实施它。在后台获取聊天消息

package com.gyaanify.groups.campfire; 

import android.app.NotificationManager; 
import android.content.Context; 
import android.net.Uri; 
import android.os.Bundle; 
import android.support.design.widget.FloatingActionButton; 
import android.support.v4.app.NotificationCompat; 
import android.support.v7.app.AppCompatActivity; 
import android.text.format.DateFormat; 
import android.view.View; 
import android.widget.EditText; 
import android.widget.ListView; 
import android.widget.TextView; 

import com.firebase.ui.database.FirebaseListAdapter; 
import com.google.android.gms.appindexing.Action; 
import com.google.android.gms.appindexing.AppIndex; 
import com.google.android.gms.appindexing.Thing; 
import com.google.android.gms.common.api.GoogleApiClient; 
import com.google.firebase.database.FirebaseDatabase; 

import static android.R.id.message; 

public class chat extends AppCompatActivity { 

    private FirebaseListAdapter<ChatMessage> adapter; 
    String domain; 
    String name; 
    String name_desig; 
    String desig; 
    String mesg; 
    /** 
    * ATTENTION: This was auto-generated to implement the App Indexing API. 
    * See https://g.co/AppIndexing/AndroidStudio for more information. 
    */ 
    private GoogleApiClient client; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_chat); 

     domain = getIntent().getStringExtra("domain"); 
     name = getIntent().getStringExtra("user"); 
     desig = getIntent().getStringExtra("desig"); 

     name_desig = name + " [" + desig + "]"; 

     displayChatMessages(); 

     FloatingActionButton fab = 
       (FloatingActionButton) findViewById(R.id.fab); 

     fab.setOnClickListener(new View.OnClickListener() { 
            @Override 
            public void onClick(View view) { 
             EditText input = (EditText) findViewById(R.id.input); 
             mesg = input.getText().toString(); 
             getmsg(); 

             // Read the input field and push a new instance 
             // of ChatMessage to the Firebase database 
             FirebaseDatabase.getInstance() 
               .getReference(domain) 
               .push() 
               .setValue(new ChatMessage(input.getText().toString(), name_desig)); 

             // Clear the input 


             input.setText(""); 
            } 
           } 
     ); 

     // ATTENTION: This was auto-generated to implement the App Indexing API. 
     // See https://g.co/AppIndexing/AndroidStudio for more information. 
     client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build(); 
    } 

    public String getmsg() { 

     return mesg; 


    } 

    private void displayChatMessages() { 

     ListView listOfMessages = (ListView) findViewById(R.id.list_of_messages); 

     adapter = new FirebaseListAdapter<ChatMessage>(this, ChatMessage.class, 
       R.layout.message, FirebaseDatabase.getInstance().getReference(domain)) { 
      @Override 
      protected void populateView(View v, ChatMessage model, int position) { 
       // Get references to the views of message.xml 
       TextView messageText = (TextView) v.findViewById(R.id.message_text); 


       String abc = messageText.getNotification().getBody(); 

       NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
         .setSmallIcon(R.mipmap.ic_launcher) 
         .setContentTitle("Firebase Push Notification") 
         .setContentText(abc) 
         .setAutoCancel(true) 
         .setSound(defaultSoundUri) 
         .setContentIntent(pendingIntent); 

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

       notificationManager.notify(0, notificationBuilder.build()); 


       TextView messageUser = (TextView) v.findViewById(R.id.message_user); 
       TextView messageTime = (TextView) v.findViewById(R.id.message_time); 

       // Set their text 
       messageText.setText(model.getMessageText()); 
       messageUser.setText(model.getMessageUser()); 

       // Format the date before showing it 
       messageTime.setText(DateFormat.format("dd-MM-yyyy (HH:mm:ss)", 
         model.getMessageTime())); 
      } 
     }; 

     listOfMessages.setAdapter(adapter); 

    } 

    /** 
    * ATTENTION: This was auto-generated to implement the App Indexing API. 
    * See https://g.co/AppIndexing/AndroidStudio for more information. 
    */ 
    public Action getIndexApiAction() { 
     Thing object = new Thing.Builder() 
       .setName("chat Page") // TODO: Define a title for the content shown. 
       // TODO: Make sure this auto-generated URL is correct. 
       .setUrl(Uri.parse("http://[ENTER-YOUR-URL-HERE]")) 
       .build(); 
     return new Action.Builder(Action.TYPE_VIEW) 
       .setObject(object) 
       .setActionStatus(Action.STATUS_TYPE_COMPLETED) 
       .build(); 
    } 

    @Override 
    public void onStart() { 
     super.onStart(); 

     // ATTENTION: This was auto-generated to implement the App Indexing API. 
     // See https://g.co/AppIndexing/AndroidStudio for more information. 
     client.connect(); 
     AppIndex.AppIndexApi.start(client, getIndexApiAction()); 
    } 

    @Override 
    public void onStop() { 
     super.onStop(); 

     // ATTENTION: This was auto-generated to implement the App Indexing API. 
     // See https://g.co/AppIndexing/AndroidStudio for more information. 
     AppIndex.AppIndexApi.end(client, getIndexApiAction()); 
     client.disconnect(); 
    } 
} 
+0

你到底有什么困惑? – iSandeep

+0

如何将此活动转移到后台服务。概述将帮助我完成这项任务。 – Sumiet

回答

1

首先,根据您的要求使用Service或IntentService扩展您的类。请在AndroidManifest.xml文件的变化,例如,你有一个类名,然后MyApp的,

<service 
    android:name=".MyApp" 
    android:exported="false">    
</service> 

然后你可以使用意图启动服务:

startService(new Intent(getApplicationContext(), MyApp.class)); 

和停止服务:

stopService(new Intent(getApplicationContext(), MyApp.class)); 
+0

此服务是否需要包含在同一个活动标签内或外部,因为在外部写入它会导致错误:.chat不能分配给服务。非常感谢兄弟。 – Sumiet

+0

@Sumiet你必须将其包含在应用程序标签内,而不是在某个活动标签内。它给出了这个错误,因为你没有用Service来扩展你的类。 –

+0

@Sumiet如果你发现我的答案有帮助,请接受它或upvote它。谢谢:) –